最新の更新情報

2012年5月
« 4月    
 12345
6789101112
13141516171819
20212223242526
2728293031  

本ブログのURL

qrcode.png

お天気

 
このブログ「吟遊詩人の戯言」 の記事検索...

【VC++】MFCにて,文字列をデミリタでぶった切る関数


キーワード(クリックで関連記事が読めます) → ,

関連記事



 
 

PHPやら,C#で言うところの,split()関数ですな

なぜか,VC++,っつうか,MFCには,気の効いた関数なりメソッドなりが,準備されていない件

とある,ソフトでさっくり作ったもんで
このまんま,眠らせておくのもなんなんで
公開しまする

バグってても,ご容赦を,ってことで

C++:
  1. void split(CString str, CString delim, CStringArray &result)
  2. {
  3.   result.RemoveAll();
  4.  
  5.     int indexback = 0;
  6.  
  7.   CString st;
  8.  
  9.   int i;
  10.   for(i = 0; i <str.GetLength(); i++)
  11.   {
  12.     if (str.GetAt(i) == delim.GetAt(0))
  13.     {
  14.       if ((i - indexback) == 0)
  15.       {
  16.         result.Add("");
  17.       }
  18.       else
  19.       {
  20.         st = str.Mid(indexback, i - indexback);
  21.         result.Add(st);
  22.       }
  23.       indexback = i + 1;
  24.     }
  25.   }
  26.  
  27.   if ((i - indexback) != 0)
  28.   {
  29.     st = str.Mid(indexback, i - indexback);
  30.     result.Add(st);
  31.   }
  32.  
  33. }
  34.  
  35. void func()
  36. {
  37.  
  38.   CString st;
  39.   CStringArray aryst;
  40.  
  41.   st = "1,2,3";
  42.  
  43.   split(st, _T(","), aryst);
  44.  
  45.   TRACE(aryst.GetAt(0));
  46.  
  47. }


こんな感じで?

 
 
 
 

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <img localsrc="" alt="">