在MFC中分割字符串,主要是利用CString函數

目的是從下列字符串提取出IP和端口號

CString  temp="227 Entering passive mode <172,18,16,176,229,252>";

void CmfcTestDlg::OnBnClickedOk()

{
    // TODO: 在此添加控件通知處理程序代碼
    CString  temp="227 Entering passive mode <172,18,16,176,229,252>";

    int base=temp.Find('<');
    int top=temp.Find('>');
    str=temp.Mid(base+1,top-base-1);
    //MessageBox(str);

    base=0;
    top=str.GetLength()-1;
    int index;
    CString store[6];
    int i=0;
    while(true)
    {
        index=str.Find(',');
        //如果找不到,返回值是-1,又在此浪費了許多時間
        if(-1==index)
        {
            break;
        }
        base=0;
        store[i]=str.Mid(base,index-base);
        MessageBox(store[i]);
        i++;

        top=str.GetLength()-1;
        str=str.Right(top-index);
        
    }
    store[i]=str;
    MessageBox(store[i]);

    CDialogEx::OnOK();
}


發佈了10 篇原創文章 · 獲贊 5 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章