2 回答

TA貢獻1876條經驗 獲得超6個贊
現在你給出的字符串已經知道了長度,而且也知道分隔位置在哪兒,直接可以用CString::Right()函數獲取后半截,如下:
CString str="abcde base64 baaaaa";
str=str.Right(6);//等式右邊得到str的后6個字符組成的字符串然后賦值給str
如果先前不知道分割點的確切位置的話,可以用如下函數查找:
CString::Find() //1
CString::FindOneOf() //2
函數1有如下幾個原型:
int Find( TCHAR ch ) const;
int Find( LPCTSTR lpszSub ) const;
int Find( TCHAR ch, int nStart ) const;
int Find( LPCTSTR lpszSub, int nStart ) const;
函數2的原型為:
int FindOneOf( LPCTSTR lpszCharSet ) const;
找到分隔點位置后就可以截取了。
與CString::Right(int n)相對的還有CString::Left(int n),它是用來截取字符串前面n個字符的

TA貢獻1895條經驗 獲得超3個贊
CString str="65.3 42.3 65 66 78.1 69";
CString temp;
int lenth=str.GetLength();
int nfrist=str.Find(" ");
while(1)
{
if(nfrist!=-1)
{
temp=str.Left(nfrist);
MessageBox(temp);
temp=str.Right(lenth-nfrist-1); //減掉1 為了去掉空格
str=temp;
nfrist=str.Find(" ");
lenth=str.GetLength();
}
else
{
MessageBox(str);
break;
}
}
沒怎么優化 按照字符串" " 查找的 建議改成特殊字符別用空串 或者空字符' ' 沒怎么優化 你自己把MessageBox 彈出來的保存就行了
- 2 回答
- 0 關注
- 101 瀏覽
添加回答
舉報