一个这样的字符串“1 ni nihao zhe ge ren”这样的字符串, 用什么方式解析能把空格去掉, 字符串放在一个数组中!

解决方案 »

  1.   

    “1 ni nihao zhe ge ren”
    用sscanf函数
      

  2.   


    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"
    ...
      

  3.   

    void SplitStrToSA(CString& inpStrSplited, CStringArray& oupStrArray, CString strSeparator = _T(";"), BOOL bIgnoreEmptyStr = TRUE);// 将字符串拆分成字符串数组///////////////////////////////////////////////////////////////
    // 函 数 名 : 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);
    }
    }
      

  4.   

    /*********************************************************************
    * 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
    }
      

  5.   

    去空格:
    CString str = _T("1 ni nihao zhe ge ren");
    while(str.Replace(_T(" "),_T("")));
      

  6.   

    用AfxExtractSubString
    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++;
    }