C++ string字串按分隔符分割成一個數組(轉)
C++的string型別可以很方便的操作字串,但是在使用中發現不支援Split,為了滿足使用的需要,我自己寫了一個分割函式。
- using std::string; //使用string物件
- using std::vector; //使用vector
-
void Split(const std::string& src, const std::string& separator, std::vector<std::string>& dest);//函式原型
- void Split(const std::string& src, const std::string& separator, std::vector<std::string>& dest) //字串分割到陣列
- {
- //引數1:要分割的字串;引數2:作為分隔符的字元;引數3:存放分割後的字串的vector向量
- string str = src;
- string substring;
- string::size_type start = 0, index;
-
dest.clear();
- index = str.find_first_of(separator,start);
- do
- {
- if (index != string::npos)
- {
- substring = str.substr(start,index-start );
- dest.push_back(substring);
- start =index+separator.size();
- index = str.find(separator,start);
- if (start == string::npos) break;
- }
- }while(index != string::npos);
- //the last part
- substring = str.substr(start);
- dest.push_back(substring);
- }
測試:
- vector<string> ster;
- vector<string>::iterator iter; //宣告一個迭代器
- string girl="春哥|胯下神駒";
- string Insta;
- CString pourin;
- Split(girl,"|",ster);
- for (iter=ster.begin();iter!=ster.end();iter++)
- {
- Insta=*iter;
- pourin=toCString(Insta);//自定義的把string轉為CString的函式
- OutputDebugString(pourin+L"\n");//除錯輸出
- }
string型別替換字串函式:
string型別的預設替換字串函式需要使用者給出需要替換的位元組數,以及替換多少次,非常詳細,但是用起來也非常麻煩,故而在網上找到一個一次性替換所有待替換字串的函式。
- void StringReplace(std::string &strBase, std::string strSrc, std::string strDes);
- void StringReplace(string &strBase, string strSrc, string strDes)
- {
- //引數1:待修改的字串;引數2:要替換的內容;引數3:替換後的內容,比如把AA替換成BB,這裡要填的是BB
- //替換完後會返回給原字串變數也就是strBase。
- string::size_type pos = 0;
- string::size_type srcLen = strSrc.size();
- string::size_type desLen = strDes.size();
- pos=strBase.find(strSrc, pos);
- while ((pos != string::npos))
- {
- strBase.replace(pos, srcLen, strDes);
- pos=strBase.find(strSrc, (pos+desLen));
- }
- }
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*
- void Brow(string diffe)//string字串轉char*
- {
- int num = strlen(diffe.c_str()) +1;//c_str()返回字串首字元地址;strlen()從第一個字元開始往後統計直到遇到結束符\0,返回數到的字元數,但不包含\0.
- //統計string字串的長度
- char* strchar1 = new char[num]; //動態建立一個字元陣列,並返回地址給char* 指標;根據string字串的長度動態申請相應的位元組陣列
- memcpy(strchar1, diffe.c_str(), num); //從引數2中把位元組拷貝到引數1中,拷貝多少個位元組由引數3指定
- if (strchar1!=NULL) //只要strchar1指標不為空用完後就釋放掉記憶體
- {
- delete []strchar1;
- strchar1 = NULL;
- }
- }
- https://blog.csdn.net/l198738655/article/details/81356915