C++ 中的字符串截取,trim

1.STL的截取字符串
 string str = "hello world";
 str.substr( 0 , 8);
輸出:
 hello wo
這裏的第一個參數是 ,起始的位置,第二個參數是截取的大小

2.char * 的合併
 char c[512] = "";
 char * c1 = "hello ";
 char * c2 = "world";
 strcat( c , c1);
 strcat( c , c2);
 printf("%s" , c);
輸出:
 hello world
注意這裏的 c必須要初始化,因爲如果不初始化 不能確定裏邊到底 初始的是什麼東西

3.STL 的trim
 void Trim( string& inout_s ) 
 { 
   // Remove leading and trailing whitespace 
   static const char whitespace[] = " \n\t\v\r\f"; 
   inout_s.erase( 0, inout_s.find_first_not_of(whitespace) ); 
   inout_s.erase( inout_s.find_last_not_of(whitespace) + 1U ); 
 }
這個函數可以將輸入字符串的 空格 換行製表符去掉,並且只去掉頭尾的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章