一、C Trim函數
C語言中的trim函數被廣泛使用來去除字元串的開頭或結尾的特定字元,比如空格和換行符。trim函數可以在標準C和C++語言中使用。下面是一個基本的C語言代碼來去除字元串兩端的空格:
#include void trim(char *str) { char *p = str; int len = strlen(p); while (isspace(*(p+len-1))) { len--; } while (isspace(*p)) { p++; len--; } memmove(str, p, len); str[len] = '\0'; }
C++中的trim函數來自於標準庫中的algorithm頭文件,它可以去除字元序列兩端的空白字元。
二、C Trim函數用法
C語言中的trim函數用於去除字元串兩端的空格。對於C++中的trim函數,首先需要include頭文件:
#include #include #include #include
然後使用trim函數去除字元串兩端的空白字元:
std::string str = " hello world "; str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](int c) { return !std::isspace(c); })); str.erase(std::find_if(str.rbegin(), str.rend(), [](int c) { return !std::isspace(c); }).base(), str.end());
以上代碼使用了C++11的Lambda表達式和標準庫函數,它會去除字元串兩端的空白字元。
三、C Trim方法
C語言中的trim函數是通過指針來修改傳入的字元串,並沒有返回值。而C++中的trim函數是通過修改字元串對象本身來去除空白字元的,因此也沒有返回值。
C++中有兩種方式來去除字元串兩端的空白字元:
- 使用std::string的成員函數
- 使用標準庫函數
第一種方式的代碼如下:
std::string str = " hello world "; str.erase(0, str.find_first_not_of(' ')); str.erase(str.find_last_not_of(' ') + 1);
以上代碼使用了std::string的成員函數find_first_not_of和find_last_not_of來查找第一個不是空格的位置和最後一個不是空格的位置,再使用erase函數來截取字元串。
第二種方式的代碼如下:
std::string str = " hello world "; str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](int c) { return !std::isspace(c); })); str.erase(std::find_if(str.rbegin(), str.rend(), [](int c) { return !std::isspace(c); }).base(), str.end());
這種方式與第二小節中的代碼相同。
四、C Trim函數功能使用
C++中的trim函數可以去除字元串兩端的空白字元,它還有其他的一些功能。例如,可以設置去除的字元為指定的字元串:
std::string str = "###hello world***"; const char* p = "#*"; str.erase(0, str.find_first_not_of(p)); str.erase(str.find_last_not_of(p) + 1);
以上代碼可以去除字元串兩端的”#”和”*”字元。
五、C Trim 正則
C++中的標準庫regex頭文件中提供了正則表達式相關的函數,我們可以使用正則表達式來去除字元串兩端的空白字元。
std::string str = " hello world "; str = std::regex_replace(str, std::regex("^\\s+|\\s+$"), "");
以上代碼使用了正則表達式”^\\s+|\\s+$”來匹配字元串兩端的空白字元,然後使用std::regex_replace函數來替換。
六、C Trim最後的末尾字元串
C++標準庫中的trim函數只能去除字元串兩端的空白字元,如果想去除字元串末尾的其他字元,可以使用find_last_not_of函數來查找最後一個不是指定字元的位置,然後使用erase函數來截取字元串。
std::string str = "hello world***"; str.erase(str.find_last_not_of("*") + 1);
以上代碼使用了find_last_not_of函數和erase函數來去除字元串末尾的”*”字元。
七、C Trim()方法
在C++17中,標準庫中提供了string_view類型,它代表一個不可變的字元串視圖,可以避免拷貝或剪切原始字元串。C++17還提供了string_view版本的trim函數。
以下是使用string_view的trim函數代碼:
#include #include std::string_view trim(std::string_view s) { auto start = s.begin(); while (start != s.end() && std::isspace(*start)) { ++start; } auto end = s.end(); do { --end; } while (std::distance(start, end) > 0 && std::isspace(*end)); return std::string_view{&*start, std::distance(start, end) + 1}; } int main() { std::string_view s = " hello world "; std::cout << '[' << s << ']' << " has length " << s.length() << '\n' << '[' << trim(s) << ']' << " has length " << trim(s).length() << '\n'; }
以上代碼使用了string_view類型的trim函數,可以去除字元串兩端的空白字元,同時保留原始字元串的視圖。
八、C Trim 多個
在C++中,有時候需要同時去除字元串兩端的多個字元,可以使用以下代碼:
#include #include std::string trim(std::string s, const char* t = " \t\n\r\f\v") { s.erase(0, s.find_first_not_of(t)); s.erase(s.find_last_not_of(t) + 1); return s; } int main() { std::string s = "\t\r ##hello world***\t\n"; std::cout << '[' << s << ']' << " has length " << s.length() << '\n' << '[' << trim(s, " \t\r#*") << ']' << " has length " << trim(s, " \t\r#*").length() << '\n'; }
以上代碼通過傳入一個指向要去除的字元的指針來去除字元串兩端的多個字元。
總結
C++中的trim函數可以去除字元串兩端的空白字元。除了標準庫提供的trim函數,我們還可以使用std::string的成員函數、標準庫函數、正則表達式等方式來去除字元串兩端的空白字元。在C++17中,我們還可以使用string_view類型的trim函數來避免字元串的拷貝和覆蓋。同時,我們也可以使用傳遞要去除的字元的字元指針的方式來去除字元串兩端的多個字元。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/247844.html