C 常用的string字串截斷函式

2022-07-04 02:12:08 字數 788 閱讀 7338

c++中經常會用到標準庫函式庫(stl)的string字串類,跟其他語言的字串類相比有所缺陷。這裡就分享下我經常用到的兩個字串截斷函式:

#include #include #include #include using namespace std;

//根據字元切分string,相容最前最後存在字元

void cutstring(string line, vector&subline, char a)

if (line[0] == a)

size_t pos = 0;

while (pos < line.length())

subline.push_back(line.substr(curpos, pos - curpos));

pos++;

} return;

}//根據空截斷字串

void chopstringlineex(string line, vector&substring)

}int main()

{ string line = ",abc,def,ghi,jkl,mno,";

vectorsubline;

char a = ',';

cutstring(line, subline, a);

cout << subline.size()《函式cutstring根據選定的字元切分string,相容最前最後存在字元;函式chopstringlineex根據空截斷字串。這兩個函式在很多時候都是很實用的,例如在讀取文字的時候,通過getline按行讀取,再用這兩個函式分解成想要的子串。

C 字串string的常用函式

include include include using namespace std intmain 輸出結果 上面所描述的是將乙個字串裡面某個區間內的大寫字母轉換為小寫字母 小寫字母轉換為大寫字母 然後儲存到另乙個空字串裡面 如 transform str1.begin str1.end str...

CSS截斷字串

方法一 任意長度的字串 說明 優點是內容可以為任何html元素,包括超連結和等,在ie6中還會在結尾自動顯示省略號。缺點是必須指定寬度數值,並且寬度不能是百分數,否則在ie中會被認為是字元總長的百分比。方法二 說明 優點是寬度可以設為百分數。但缺點是內容只能為純文字,不能有超連結等內容。css 截斷...

String字串常用方法

字串間比較 equals 和contentequals equals string 和 string 比較 equalsignorecase 不考慮大小寫 contentequals stirng 和stringbuffer stringbuilder比較 返回查詢字元或字串的索引 indexof ...