文字列関数

  文字列の連結




  • ライブラリ関数
  • strcpy( a, "ABC" );
  • strcat( a, "DEF" );

  • API関数
  • lstrcpy( a, "ABC" );
  • lstrcat( a, "DEF" );



  文字列の比較




  • char *a = "ABC"

  • ライブラリ関数
  • strcmp( a, "ABC" ) ==> 0
  • stricmp( a, "abc" ) ==> 0 ( strcmpi も同じ )

  • API関数
  • lstrcmp( a, "ABC" ) ==> 0
  • lstrcmpi( a, "abc" ) ==> 0



  文字列の長さ


  • char *a = "ABC"

  • ライブラリ関数
  • strlen( a ) ==> 3

  • API関数
  • lstrlen( a ) ==> 3



  長さ指定関数


  • ライブラリ関数
  • strncpy( a, "ABC", 2 ); ==> "AB" で 2 (NULL はセットされない)

  • API関数
  • lstrcpyn( a, "ABC", 2 ); ==> "A" と NULL で 2

  • Shell Utility API関数 (要 #include "shlwapi.h")
  • StrCpyN( a, "ABC", 2 ); ==> "A" と NULL で 2

  
char a[4] = {255,255,255,255};

strncpy( a, "ABC", 1 );
printf( "%x\n", 0x000000ff & a[1] );

strncpy( a, "ABC", 2 );
printf( "%x\n", 0x000000ff & a[1] );

lstrcpyn( a, "ABC", 2 );
printf( "%x\n", 0x000000ff & a[1] );
  

以下処理結果
  
ff
42
0
  


  • ライブラリ関数
  • strncmp( a, "ABC", 2 ); ==> a と "AB" を比較
  • strnicmp( a, "abcxyz", 3 )

  • Shell Utility API関数 (要 #include "shlwapi.h")
  • StrCmpN( a, "ABC", 2 ); ==> a と "AB" を比較
  • StrnCmpNI( a, "abcxyz", 3 )



  Shell Utility API の特殊な関数

指定文字列集合に含まれる文字の位置
  
#include "stdafx.h"
#include <windows.h>
#include <shlwapi.h>
#include <stdio.h>

#pragma comment( lib, "shlwapi.lib" )

int main( int argc, char *argv[] )
{
	char *a = "ABC1XYZ";
	int pos;
	
	pos = StrCSpn( a, "1234568790" );

	printf( "%d\n", pos );

	return 0;
}
  


指定文字列集合に含まれない文字の位置
  
#include "stdafx.h"
#include <windows.h>
#include <shlwapi.h>
#include <stdio.h>

#pragma comment( lib, "shlwapi.lib" )

int main( int argc, char *argv[] )
{
	char *a = "ABC1XYZ";
	int pos;
	
	pos = StrSpn( a, "ABCXYZ" );

	printf( "%d\n", pos );

	return 0;
}
  


指定文字列位置の検索
  
#include "stdafx.h"
#include <windows.h>
#include <shlwapi.h>
#include <stdio.h>

#pragma comment( lib, "shlwapi.lib" )

int main( int argc, char *argv[] )
{
	char *a = "ABC1XYZ";
	char *pos;
	
	pos = StrStr( a, "XY" );

	printf( "%d\n", pos - a );

	return 0;
}
  
LboxString.Search


文字列の両側から指定文字集合に含まれる文字(連続している)を取り除く
  
#include "stdafx.h"
#include <windows.h>
#include <shlwapi.h>
#include <stdio.h>

#pragma comment( lib, "shlwapi.lib" )

int main( int argc, char *argv[] )
{
	char a[20] = "aabbccX0Y0Z001122";
	char *pos;
	
	StrTrim( a, "abc012" );

	printf( "%s\n", a );

	return 0;
}
  
LboxString.Trim










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ