C 文字列操作 新しいページはコチラ
提供: yonewiki
(→文字列の検索と置換) |
(→文字列の検索と置換) |
||
4,071行: | 4,071行: | ||
− | + | 部分一致の処理は難しいので、typedef関数とクラスと構造体のコンストラクタとテンプレートクラスとクラスのメンバ関数およびクラスのメンバ変数、更には標準クラスであるvectorクラスとpair構造体について理解してから戻ってくるとよいかもしれません。 | |
+ | |||
+ | ICUを使わない文字列検索としてはstrchのような***ch系の検索関数があります。見つかった位置のByte数を返してくれます。ICUの正規表現に比べると機能性は低いです。一文字を引数にするとき符号なし整数型にしなければいけないあたりは、初期値を与えての検索は楽ですが、実際に一文字の文字列から検索しようとすると大変なのかもしれません。実際に数えてみないと、一文字あたりが、1バイトなのか2バイトなのかわからないし…。総合的に検索を支援してくれるICUの正規表現がいかに楽なのかがわかります。 | ||
+ | |||
+ | <syntaxhighlight lang="cpp" line start="1"> | ||
+ | #pragma once | ||
+ | #include "stdafx.h" | ||
+ | #include <stdlib.h> | ||
+ | #include <string> //std::string型定義 | ||
+ | #include <mbstring.h> //mbs***関数 | ||
+ | #include <iostream> //cpp cout etc 一般入出力関数 | ||
+ | #include <locale> //Locale関数 | ||
+ | #include <tchar.h> //TCHAR型+_tcs***関数 | ||
+ | #include "atlstr.h" //CString,CStringA,CStringWおよびLPTSTR,LPWSTR,LPSTR,LPCTSTR,LPCWSTR,LPCSTRの定義プリプロセッサ | ||
+ | #include "atlbase.h" //同上 | ||
+ | #include "cstringt.h" //CStringTの定義プリプロセッサ | ||
+ | #include <vector> | ||
+ | |||
+ | |||
+ | //_bstr_t型を利用するプリプロセッサ | ||
+ | #include "comutil.h" | ||
+ | #ifdef _DEBUG | ||
+ | #pragma comment(lib, "comsuppwd.lib") | ||
+ | #else | ||
+ | #pragma comment(lib, "comsuppw.lib") | ||
+ | #endif | ||
+ | //_bstr_t | ||
+ | |||
+ | //ICU ucnvプリプロセッサ | ||
+ | #include <unicode/ucnv.h> | ||
+ | #include <unicode/translit.h> | ||
+ | #include <unicode/regex.h> | ||
+ | #ifdef _DEBUG | ||
+ | //#pragma comment(lib, "icudt.lib") | ||
+ | #pragma comment(lib, "icuind.lib")//ICU Transliterate関数を使うために必要なライブラリ 正規表現関数も | ||
+ | #pragma comment(lib, "icuucd.lib")//ICU ucnvを使うために必要なライブラリ | ||
+ | //#pragma comment(lib, "icuiod.lib") | ||
+ | #else | ||
+ | #pragma comment(lib, "icudt.lib") | ||
+ | #pragma comment(lib, "icuind.lib") | ||
+ | #pragma comment(lib, "icuuc.lib") | ||
+ | #pragma comment(lib, "icuio.lib") | ||
+ | #endif | ||
+ | |||
+ | |||
+ | #include "UnicodeConverter.h" | ||
+ | |||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | int _tmain(int argc, _TCHAR* argv[]) | ||
+ | { | ||
+ | |||
+ | int SingleChr = 'r'; | ||
+ | unsigned int mbSingleString; | ||
+ | unsigned int nKeta; | ||
+ | |||
+ | char pcSingleStr[] = "列"; | ||
+ | |||
+ | unsigned char mbstring[] = "日本語の検索用文字列です。"; | ||
+ | |||
+ | char pcString[] = "日本語の検索用文字列です。The quick brown dog jumps over the lazy fox"; | ||
+ | char fmt1[] = " 1 2 3 4 5 6"; | ||
+ | char fmt2[] = "123456789012345678901234567890123456789012345678901234567890123456789"; | ||
+ | |||
+ | char *pcPos; | ||
+ | unsigned char *pucPos; | ||
+ | unsigned char *pucRusultPos; | ||
+ | |||
+ | int result; | ||
+ | int result2; | ||
+ | |||
+ | printf_s( "文字列検索:\n\n %s\n", pcString ); | ||
+ | printf_s( " %s\n %s\n\n", fmt1, fmt2 ); | ||
+ | |||
+ | |||
+ | |||
+ | //★マルチバイト対応1文字の文字列で検索。 | ||
+ | printf_s( "★マルチバイト対応検索\n"); | ||
+ | |||
+ | pucPos = (unsigned char*)pcSingleStr; | ||
+ | pucPos = _mbsinc(pucPos);//一文字分ポインタを進める関数 | ||
+ | result = (int)(pucPos - (unsigned char*)pcSingleStr); | ||
+ | mbSingleString = 0; | ||
+ | nKeta = 1; | ||
+ | for(int n = 0; n < result; n++){ | ||
+ | mbSingleString = mbSingleString + (*(pucPos - (n + 1))) * (nKeta); | ||
+ | printf_s( "%02x \n", (*(pucPos - (n + 1)))); | ||
+ | nKeta = nKeta * 0x100; | ||
+ | } | ||
+ | |||
+ | //printf_s( "%02x%02x\n", *(pucPos - 2), *(pucPos - 1));このカタチをunsigned int の変数に変形したのが mbSingleString | ||
+ | //unsigned int mbstringString = 'の';とかでも検索できますが、strcpy関数とかでコピーしてきた一文字の文字列でサンプル | ||
+ | |||
+ | printf_s( "MultiByteString検索文字 SigngleWord:%04x(%s)\n", mbSingleString, pcSingleStr); | ||
+ | printf_s( "incriment:%d[Byte]\n\n",result); | ||
+ | |||
+ | pucRusultPos = _mbschr((unsigned char*)pcString, mbSingleString); | ||
+ | result2 = (int)(pucRusultPos - (unsigned char*)pcString + 1); | ||
+ | if ( pucRusultPos != NULL ) | ||
+ | printf_s( "検索結果: 最初の %04x(%s) が見つかった位置は %d[Byte]目\n", mbSingleString, pcSingleStr, result2 ); | ||
+ | else | ||
+ | printf_s( "検索結果: %s not found\n\n", pcSingleStr ); | ||
+ | |||
+ | |||
+ | //★シングルバイト文字検索。 | ||
+ | printf_s( "\n\n"); | ||
+ | printf_s( "★シングルバイト文字の初期値による検索\n"); | ||
+ | printf_s( "検索文字: %c\n", SingleChr ); | ||
+ | |||
+ | |||
+ | //前方検索 | ||
+ | pcPos = strchr( pcString, SingleChr ); | ||
+ | result = (int)(pcPos - pcString + 1); | ||
+ | if ( pcPos != NULL ) | ||
+ | printf_s( "検索結果: 最初の %c が見つかった位置は %d[Byte]目\n", SingleChr, result ); | ||
+ | else | ||
+ | printf_s( "検索結果: %c not found\n", SingleChr ); | ||
+ | |||
+ | |||
+ | //後方検索 | ||
+ | pcPos = strrchr( pcString, SingleChr ); | ||
+ | result = (int)(pcPos - pcString + 1); | ||
+ | if ( pcPos != NULL ) | ||
+ | printf_s( "検索結果: 最後に %c が見つかった位置は %d[Byte]目\n", SingleChr, result ); | ||
+ | else | ||
+ | printf_s( "検索結果:\t%c not found\n", SingleChr ); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | |||
+ | </syntaxhighlight> | ||
=='''文字列ファイルパス操作'''== | =='''文字列ファイルパス操作'''== |