1. 程式人生 > 其它 >C++ string字串按分隔符分割成一個數組(轉)

C++ string字串按分隔符分割成一個數組(轉)

C++的string型別可以很方便的操作字串,但是在使用中發現不支援Split,為了滿足使用的需要,我自己寫了一個分割函式。

  1. #include <string>
  2. #include <vector>
  3. using std::string; //使用string物件
  4. using std::vector; //使用vector
  5. void Split(const std::string& src, const std::string& separator, std::vector<std::string>& dest);//函式原型
  6. void Split(const std::string& src, const std::string& separator, std::vector<std::string>& dest) //字串分割到陣列
  7. {
  8. //引數1:要分割的字串;引數2:作為分隔符的字元;引數3:存放分割後的字串的vector向量
  9. string str = src;
  10. string substring;
  11. string::size_type start = 0, index;
  12. dest.clear();
  13. index = str.find_first_of(separator,start);
  14. do
  15. {
  16. if (index != string::npos)
  17. {
  18. substring = str.substr(start,index-start );
  19. dest.push_back(substring);
  20. start =index+separator.size();
  21. index = str.find(separator,start);
  22. if (start == string::npos) break;
  23. }
  24. }while(index != string::npos);
  25. //the last part
  26. substring = str.substr(start);
  27. dest.push_back(substring);
  28. }

測試:

  1. vector<string> ster;
  2. vector<string>::iterator iter; //宣告一個迭代器
  3. string girl="春哥|胯下神駒";
  4. string Insta;
  5. CString pourin;
  6. Split(girl,"|",ster);
  7. for (iter=ster.begin();iter!=ster.end();iter++)
  8. {
  9. Insta=*iter;
  10. pourin=toCString(Insta);//自定義的把string轉為CString的函式
  11. OutputDebugString(pourin+L"\n");//除錯輸出
  12. }

string型別替換字串函式:

string型別的預設替換字串函式需要使用者給出需要替換的位元組數,以及替換多少次,非常詳細,但是用起來也非常麻煩,故而在網上找到一個一次性替換所有待替換字串的函式。

  1. void StringReplace(std::string &strBase, std::string strSrc, std::string strDes);
  2. void StringReplace(string &strBase, string strSrc, string strDes)
  3. {
  4. //引數1:待修改的字串;引數2:要替換的內容;引數3:替換後的內容,比如把AA替換成BB,這裡要填的是BB
  5. //替換完後會返回給原字串變數也就是strBase。
  6. string::size_type pos = 0;
  7. string::size_type srcLen = strSrc.size();
  8. string::size_type desLen = strDes.size();
  9. pos=strBase.find(strSrc, pos);
  10. while ((pos != string::npos))
  11. {
  12. strBase.replace(pos, srcLen, strDes);
  13. pos=strBase.find(strSrc, (pos+desLen));
  14. }
  15. }

LPCTSTR不是一個型別,而是兩種型別:LPCSTR和LPCWSTR其中之一。會根據你當前程式是否使用UNICODE字符集來變成那二者之一。如果使用UNICODE字符集,則LPCTSTR = LPCWSTR,否則LPCTSTR = LPCSTR。

標準庫的std::string轉換成LPCSTR很簡單:直接呼叫c_str()即可。例:

std::string a="abc";

LPCSTR str = a.c_str();

標準庫還有一個wstring,代表寬字元的string,std::wstring轉換成LPCWSTR也一樣很簡單:

std::wstring a = L"abc";

LPCWSTR str = a.c_str();

如果要是std::string轉換成LPCWSTR或者std::wstring轉換成LPCSTR那就比較麻煩了,需要呼叫MultiByteToWideChar或WideCharToMultiByte進行 字符集之間的轉換。不過大多數時候不需要這種交叉轉換,一個程式一般只會使用一種字符集。

C++或MFC中關於轉義字元的處理

D:\XKey\XKey.exe

比如上面這個地址,按照C++編譯器的標準"\"屬於轉義符,需要替換成"\\"來表示,但這只是在C++的原始碼中需要這樣表示,如果用API讀取文字中的地址,編譯後\就還是\不需要再替換成\\。

也就是說只有在程式碼中寫的"D:\\XKey\\XKey.exe"這種地方才會有轉義字元,真正程式編譯完畢之後就不存在轉義的問題了。程式編譯完畢之後,上面的字串就變為了C:\1.txt。另外在VC++中也可以用"/"替代"\\"來表示目錄層次,比如"D:/XKey/XKey.exe"也是可以編譯通過正常使用的,此時就不需要把"/"改成"//"了。

string字串轉char*

    1. void Brow(string diffe)//string字串轉char*
    2. {
    3. int num = strlen(diffe.c_str()) +1;//c_str()返回字串首字元地址;strlen()從第一個字元開始往後統計直到遇到結束符\0,返回數到的字元數,但不包含\0.
    4. //統計string字串的長度
    5. char* strchar1 = new char[num]; //動態建立一個字元陣列,並返回地址給char* 指標;根據string字串的長度動態申請相應的位元組陣列
    6. memcpy(strchar1, diffe.c_str(), num); //從引數2中把位元組拷貝到引數1中,拷貝多少個位元組由引數3指定
    7. if (strchar1!=NULL) //只要strchar1指標不為空用完後就釋放掉記憶體
    8. {
    9. delete []strchar1;
    10. strchar1 = NULL;
    11. }
    12. }
  1. https://blog.csdn.net/l198738655/article/details/81356915