一、
split()方法用於將一個字符串分割成字符串數組。
語法:
stringObject.split(separator,howmany)
- 1
- 2
參數介紹:separator:必需。字符串或正則表達式,從該參數指定的地方分割stringObject。
howmany
可選,該參數指定返回的數組的最大長度。如果設置了該參數,返回的子串不會多餘這個參數指定的數組。如果沒有設置該參數,整個字符串都會被分割,不考慮它的長度。
返回值:一個字符串數組,該數組是通過在separator指定的邊界處將字符串stringObject分割成子串創建的。返回的數組中的子串不包括separator自身。
但是如果separator是包含子表達式的正則表達式,那麼返回的數組中包括與這些子類表達式匹配的子串(不包括與整個正則表達式匹配的文本)。
注意:如果把空字符(“”)用來separator,那麼stringObject中的每個字符之間都會被分割。
例如:
string str="hello";
string[] a=str.split("");
for(int i=0;i<a.size();i++)
{cout<<a[i]<<endl;}
- 1
- 2
- 3
- 4
結果:
h
e
l
l
o
例1:指定字符分割
string str="123@456";
string[] a=str.split("@");
cout<<a[0]<<", "<<a[1]<<endl;
- 1
- 2
- 3
- 4
輸出結果爲:123, 456
例2、使用下面的方法可以把句子分割成單詞
var word=sentence.split(’ ‘);
或者 使用正則表達式作爲separator:
var words=sentence.split(/\s+/);
對於某些特殊字符,如果字符串正好是正則的一部分,那麼就需要轉義才能使用。
這些字符有*|,+,,^,$, / , |, [, ], (, ), -, ., **等
因爲他們是正則表達式中的一部分,所以如果想要用該字符本身,這些字符需要進行轉移才能表示它本身,
例3、
想要用|豎線去分割某字符,因爲|本身是正則表達式中的一部分,所以需要\去轉義,而\正好也是正則表達式中的字符,所以還需要一個\,既需要使用兩個。\纔可以。
string str="123|abc";
string []b=str.split("\\|");
cout<<b[0]<<","<<b[1]<<endl;
- 1
- 2
- 3
輸出結果爲:123, abc
例4、如果希望把字符串只返回一部分字符,就使用howmany參數:
“hello”.split(“”,3)
結果就返回“h”,”e”,”l”
二、在c++中strtok()用來將字符分割成一個個片段。參數str指向欲分割的字符串,參數delimiters則爲分割字符串,當strtok()在參數str的字符串中發現到參數的delimiters的分割字符時會將該字符改爲‘\0’字符,在第一次調用時,strtok()必須給予參數str字符串,往後的調用則將參數str設置成NULL.
函數原型:
char*strtok(char*str,const char *delimiters);
- 1
- 2
例1、
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="a,b,c,d*e";
const char * split = ",";
char * p;
p = strtok (str,split);
while(p!=NULL) {
printf ("%s\n",p);
p = strtok(NULL,split);
}
getchar();
return 0;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
輸出:
a
b
c
d*e
一. _tcstok 函數
在 MFC 中,一個 CString str=“ab ac dd PM” ,怎麼把他分開成 ab 和 ac 和 dd 和 PM 這四個字符串,類似 string 的 split的功能?
_tcstok 聲明頭文件 : <TCHAR.H>
char * _tcstok ( char * strToken , const char * strDelimit );
該函數是可以從一個 CString 串中,根據提供的分隔符,截取並返回一個一個的 Token ;
參數 :
strToken: 是一個要分析的串;這個串中包含一個或者多個 Token ,當然還有分隔符,也有可
能有其他的字符;
strDelimit: 是分隔符;根據分隔符把 strToken 中的 Token 分析出來;
//==============test1:_tcstok===============
// 將以空格符爲分隔符對str 進行分割
CString str = _T ("192.168.89.125" );
TCHAR seps [] = _T ("." );
TCHAR * token = _tcstok ( (LPTSTR )(LPCTSTR )str , seps );
while ( token != NULL )
{
printf ("str=%s token=%s/n" ,str ,token );
token = _tcstok ( NULL , seps );
}
//==============test1:end==================
執行結果如下:
str=192 token=192
str=192 token=168
str=192 token=89
str=192 token=125
第一次調用的時候,函數會忽略出現在 strToken 串開始的分隔符,返回找到的 Token 指針,用空字符( NULL character )替換掉已經查找到的部分(包括分隔符)並把 “ 新 ” 串保存到一個 Static 變量中(系統來完成);
如果下次調用時第一個參數爲 NULL 的話,函數從 Static 變量中取出串,根據分隔符得到並返回新 Token ,用空字符( NULL character )替換掉已經查找到的部分(包括分隔符)並重新保存 “ 新 ” 串;如此循環,直到循環條件結束。
參考 : http://biao3730.spaces.live.com/Blog/cns!3415F543A07A7AE5!175.entry
也 可以使用 char * __cdecl strtok (char *, const char *);( 包含在頭文件<string.h> 中)
參考: http://baike.baidu.com/view/1028553.htm
二. AfxExtractSubString 函數
此外,用 AfxExtractSubString 函數可解析複合串。包含在頭文件 < AFXWIN.H > 中。
BOOL AfxExtractSubString (CString & rString , LPCTSTR lpszFullString , int iSubString , TCHAR chSep /* = */ )
參數 :
rString :用來存放你取出的子串
lpszFullString :要拆分的整個字符串
iSubString :你要取的子字符串位置,從 0 開始
chSep :特定分割符
//========test2:AfxExtractSubString========
CString str = _T ("192.168.89.125" );
CString output = "" ;
for (int i =0; i <4; i ++)
{
AfxExtractSubString (output , str , i , '.' );
printf ("%s/n" ,output );
}
//==============test2:end==================
執行結果如下:
192
168
89
125
參考 : http://www.vckbase.com/bbs/prime/viewprime.asp?id=353
三.自定義函數字符串分割函數
void Split (CString source , CString divKey, CStringArray & dest )
{
dest .RemoveAll ();
int pos = 0;
int pre_pos = 0;
while ( -1 != pos ){
pre_pos = pos ;
pos = source .Find (division ,(pos +1));
dest .Add (source .Mid (pre_pos ,(pos -pre_pos )));
}
}
參數:
Source :待分割的源串
divKey: 分割符
dest: 分割結果字符串數組