一个这样的字符串“1 ni nihao zhe ge ren”这样的字符串, 用什么方式解析能把空格去掉, 字符串放在一个数组中!
解决方案 »
- 百度云盘有API? CSDN网盘竟然用的是百度云盘
- insert写数据库问题
- D3D 纹理转换到DC
- socket编程,服务器会用bind()绑定端口,而客户端不用绑定端口就可以调用connect()。那么客户端的端口时系统自己分配的么?是何时绑定的?
- 大家帮我看看这段。
- ASKER
- 怎解决1024*768下用MM_TEXT画棋盘在800*600下的显示?
- .rtf文件应该用什么打开?它显示的是word文档的图标
- 哪里有原版Efficient C++??
- 串口通信中怎样将串口得到的数据存入到数据库中?
- 如何将两个中文字节合并为一个中文字
- 高分求解!!! 如何将Int转换为char[]类型???
用sscanf函数
CStringArray arrTemp;
CString strTemp = "1 ni nihao zhe ge ren";
char *ss=strTemp.GetBufferSetLength(strTemp.GetLength()); char *p;
for ( p=strtok(ss, " "); p!=NULL; p=strtok(NULL, " ") ) { arrTemp.Add(p); }结果arrTemp[0] = "1";
arrTemp[1] = "ni"
...
// 函 数 名 : SplitStrToArray
// 所属类名 : HeslUtils
// 函数功能 : 将字符串拆分成字符串数组
// 处理过程 :
// 备 注 : “@& abc@&de @&”按“@&”的拆分结果——》
// (1)忽略空字符串:abc,de(拆分后数组长度为2)
// (2)不略空字符串:, abc,de ,(拆分后数组长度为4)
// 作 者 : hesl
// 时 间 : 2005年5月13日
// 返 回 值 : void
// 参数说明 : CString& inpStrSplit——[in]被拆分的字符串
// CStringArray& oupStrArray——[out]记录拆分结果
// CString strSeparator /*= _T(";")*/——[in]分隔符
// BOOL bIgnoreEmptyStr /*= TRUE*/——[in]是否忽略空字符串
///////////////////////////////////////////////////////////////
void SplitStrToSA(CString& inpStrSplited, CStringArray& oupStrArray, CString strSeparator /*= _T(";")*/, BOOL bIgnoreEmptyStr /*= TRUE*/)
{
oupStrArray.RemoveAll();
CString strSplited = inpStrSplited;
// 依次拆分
int nPos = strSplited.Find(strSeparator);
CString strTemp;
while(nPos != -1)
{
strTemp = strSplited.Left(nPos);
if(bIgnoreEmptyStr)
// 忽略空字符串
{
strTemp.TrimLeft();
strTemp.TrimRight();
if(!strTemp.IsEmpty())
{
oupStrArray.Add(strTemp);
}
}
else
// 不忽略空字符串
{
oupStrArray.Add(strTemp);
}
strSplited = strSplited.Mid(nPos + strSeparator.GetLength());
nPos = strSplited.Find(strSeparator);
}
// 处理最后一截
if(bIgnoreEmptyStr)
// 忽略空字符串
{
strSplited.TrimLeft();
strSplited.TrimRight();
if(!strSplited.IsEmpty())
{
oupStrArray.Add(strSplited);
}
}
else
// 不忽略空字符串
{
oupStrArray.Add(strSplited);
}
}
* Proc: CStr::GetWindowText and GetDlgItemText
* Purpose: Helpers for MFC programs
*********************************************************************/#ifndef CSTR_NO_WINSTUFF
void CStr::GetWindowText (CWnd* wnd)
{
Empty();
// How big a buffer should we have?
HWND w = wnd->GetSafeHwnd();
CPOS tl = (CPOS) ::SendMessage (w, WM_GETTEXTLENGTH, 0, 0L);
if (tl == 0)
return;
Buffer((CPOS) (tl+2));
// And get the text
::SendMessage (w, WM_GETTEXT, (WPARAM) tl+1, (LPARAM) data->m_Text);
data->m_Length = tl;
}
#endif/*********************************************************************
* Proc: CStr::LTrim
* Purpose: Remove leading characters from a string. All characters
* to be excluded are passed as a parameter; NULL means
* 'truncate spaces'
*********************************************************************/void CStr::LTrim(const char* charset /*= NULL*/)
{
CPOS good = 0;
if (charset) {
while (strchr (charset, data->m_Text[good]) != NULL)
good++;
}
else {
while (data->m_Text[good] == ' ')
good++;
}
if (good > 0)
RemoveLeft (good);
}
/*********************************************************************
* Proc: CStr::Trim
* Purpose: Remove trailing characters; see LTrim
*********************************************************************/void CStr::Trim(const char* charset /*= NULL*/)
{
CPOS good = data->m_Length;
if (good == 0)
return;
if (charset) {
while (good > 0 && strchr (charset, data->m_Text[good-1]) != NULL)
--good;
}
else {
while (good > 0 && data->m_Text[good-1] == ' ')
--good;
}
TruncateAt (good); // Also works well with good == 0
}
CString str = _T("1 ni nihao zhe ge ren");
while(str.Replace(_T(" "),_T("")));
CString strArr[n]; //也可以用CStringArray
CString szTemp;
CString szSource = "1 ni nihao zhe ge ren";
int i = 0;while(AfxExtractSubString( szTemp, strSource, i, " "))
{
strArr[i] = szTemp;
i++;
}