读书人

!字符串分割的

发布时间: 2012-10-24 14:15:58 作者: rapoo

紧急求助!!!!!字符串分割的
第一次弄 vc++

1 208 413
2 526 792
3 572 794
4 620 796

每个数字中间的 是 空格,数量不定

请问如何把 这每一行的数字 取出来!!


多谢!

[解决办法]
char string[]="1 208 413";
char *token;
token=strtok(string," ");
while (token !=NULL)
{
token=strtok(NULL," ");
}
[解决办法]
CString str = "1 208 413";
int i1, i2 ,i3 ;
sscanf(str , "%d %d %d" , &i1 , &i2 , &i3);
[解决办法]
两种理解啊,第一种就是取出一行数字,除去空格;另外一种是空格前后的数字分别取出来
第一种很容易实现
CString str="222 333 4444";
str.Remove(' ');

结果就是str="2223334444"

第2种情况:
CString str="222 333 4444";
CStringArray strarray;
CString str1;
for( ; ; )
{
str1=str.SpanExcluding(" ");
strarray.add(str1);
str=str.Right(str.GetLenght()-str1.GetLenght()-1);
if(str1==str)
return;
}
每个空格前后的数字就存在strarray中了。




[解决办法]
在VC++中有个API函数:AfxExtractSubString(ChildString,GlobalString,i,' ');

具体看看msdn
[解决办法]
以前收集的一个函数,自己也写过STL版本的,可以查看我BLOG里面,某500强公司的考核试题

C/C++ code
INT_PTR C2COMTo1COMForVIDlg::Split_CString( CString& source, //需要截取的原字符串CStringArray& dest, //分割后的字符串数组const CString& division //用做分割符的字符串){  if( source.IsEmpty() )  return -1;  dest.RemoveAll();  int len = division.GetLength();  int iFirst = 0;  int nCount = 0;  int pos = 0;  int pre_pos = -1;  while( -1 != pos )  {  if( -1 == pre_pos ){  pos = source.Find(division,pos);}  else{  pos = source.Find(division,(pos+1));}  if( -1 == pre_pos )  {  iFirst = 0;  if( -1 == pos )  nCount = source.GetLength();  else  nCount = pos;  }  else  {  iFirst = pre_pos+len;  if( -1 != pos )  nCount = pos - pre_pos - len;  else  nCount = source.GetLength()-pre_pos-len;  }  dest.Add(source.Mid(iFirst,nCount));  pre_pos = pos;  }  return dest.GetCount();} 

读书人网 >VC/MFC

热点推荐