Boost(五)——字符串處理(一):字符串操作

結合Boost官網

由於這一章內容過多,我將採用三個小章,精簡原文三個小部分內容。

區域設置:

setlocale(LC_ALL,“”)
locale::global(std::locale("German")); //設置全局區域德語環境

字符串操作:

一、將字符串所有字符轉成大寫

boost::algorithm::to_upper("")//自身轉化
boost::algorithm::to_upper_copy("")//返回轉化的結果,自身不轉化

轉成小寫

boost::algorithm::to_lower("")
boost::algorithm::to_lower_copy("")

二、刪除特定字符串

boost::algorithm::erase_first_copy(s,"") //從s刪除第一個匹配的字符串
boost::algorithm::erase_nth_copy(s,"",n) //從s刪除第n個匹配的字符串
boost::algorithm::erase_last_copy(s, "") //從s刪除最後匹配的字符串
boost::algorithm::erase_all_copy(s, "") //從s刪除所有匹配的字符串
boost::algorithm::erase_head_copy(s, n) //從s刪除前n個字符
boost::algorithm::erase_tail_copy(s, n) //從s刪除後n個字符

三、查找特定字符串

與二相同使用方法,將erase替換成find即可。

四、字符串迭代器

存儲字符串每個字符。

boost::iterator_range<string::iterator> r = boost::algorithm::find_first(s,"");
for_each(r.begin(), r.end(), [](char c){cout << c << endl;});

五、添加字符串

vector<string> v;
v.push_back("hello");
v.push_back("world");
boost::algorithm::join(v, ""); //根據第二個參數將這些字符串連接起來。

六、替換字符串

boost::algorithm::replace_first_copy(s,"","") //從s替換第一個匹配的字符串成第三個參數
boost::algorithm::replace_nth_copy(s,"",n,"") //從s替換第n個匹配的字符串成第四個參數
boost::algorithm::replace_last_copy(s, "","") //從s替換最後匹配的字符串成第三個參數
boost::algorithm::replace_all_copy(s, "","") //從s替換所有匹配的字符串成第三個參數
boost::algorithm::replace_head_copy(s, n,"") //從s替換前n個字符成第三個參數
boost::algorithm::replace_tail_copy(s, n,"") //從s替換後n個字符成第三個參數

七、裁剪字符串

自動裁剪:(空格或者結束符)

boost::algorithm::trim_left_copy(s)//去除左邊
boost::algorithm::trim_right_copy(s)//去除右邊
boost::algorithm::trim_copy(s)//上兩個效果合

特定裁剪:指定字符串裁剪

boost::algorithm::trim_left_copy_if(s, boost::algorithm::is_any_of(""))//去除左邊
boost::algorithm::trim_right_copy_if(s, boost::algorithm::is_any_of(""))//去除右邊
boost::algorithm::trim_copy_if(s, boost::algorithm::is_any_of(""))//上兩個效果合

函數boost::algorithm::is_digit() 返回的謂詞在字符爲數字時返回布爾值 。

裁剪謂詞是數字的字符串:

boost::algorithm::trim_left_copy_if(s, boost::algorithm::is_digit())//去除左邊
boost::algorithm::trim_right_copy_if(s, boost::algorithm::is_digit())//去除右邊
boost::algorithm::trim_copy_if(s, boost::algorithm::is_digit())//上兩個效果合

八、比較字符串

boost::algorithm::starts_with(s, "")//比較開頭
boost::algorithm::ends_with(s, "")//比較結尾
boost::algorithm::contains(s,"" )//比較是否存在
boost::algorithm::lexicographical_compare(s,"")/比較區間是否小於第二個參數

返回爲bool型 

九、分割字符串

boost::algorithm::split(s, 謂詞);

謂詞:判定分割點

例如:boost:algorithm::is_space() //在每個空格字符處分割字符串

十、大小寫忽略函數

一般是上述函數原型前有i區分

例如 boost::algorithm::erase_all_copy() 對應 boost::algorithm::ierase_all_copy()。

 

 

 

 

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