您的位置:首页 > 编程语言 > C语言/C++

C++ 字符串 处理 消除多余空格

2012-02-16 15:08 274 查看
////消除多余空格
void CStringTestDlg::OnBnClickedOk()
{
CString str("i am    a  stu aa aa   ddd  s");
CString rlt = RemoveExtraSpace(str);
AfxMessageBox(rlt);
// TODO: Add your control notification handler code here
//OnOK();
}

CString CStringTestDlg::RemoveExtraSpace(CString str){
CString ret;
int i = 0;
bool lastChrIsSpace = false;
for(i = 0;i < str.GetLength() ;i ++){

if(str[i] == ' '){
if(lastChrIsSpace){
continue;
}
else{
ret.AppendChar(' ');
lastChrIsSpace = true;
}
}
else{
ret.AppendChar(str.GetAt(i));
lastChrIsSpace = false;
}
}
return ret;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++