boost庫中的字符串算法查找、切割、替換、刪除、合併、大小寫轉換等等詳解
零、小序
字符串操作是每一個程序員幾乎每天都要進行的工作,很多的開發工作中都要用到字符串的操作,無論是網絡通訊、界面顯示還是寫日誌等操作,大多數程序員都喜歡搞成字符串的形式來進行操作。標準的C++對字符串的操作已經提供了很多方法,但還是有很多不盡人意的地方,例如對字符串切割,大小寫轉換、替換、刪除等等操作,使用boost進行處理會更加的方便,boost庫提供的支持更加的齊全,沒有boost庫做不到的,只有你想不到的!
一、大小寫轉換
1、大小寫轉換函數
boost提供的大小寫轉換函數有boost::algorithm::to_upper_copy和boost::algorithm::to_lower_copy、boost::algorithm::to_upper和boost::algorithm::to_lower這兩組大小寫轉換的形式。前一組的大小寫轉換不會改變原來的字符串,會返回一個原來字符串的拷貝,後面一組大小寫轉換會改變原來的字符串的大小寫。
細心的朋友會發現上面這兩組大小寫轉換會提供的有兩個參數,第一個是傳入要轉換的字符串,第二個是有關區域設置的,這個區域設置一般默認就行,除非你開發的軟件具有區域性,比如開發的軟件給外國人使用,你可能需要設置成那個國家的區域標誌,以適合那個國家的文化需要。
2、大小寫代碼示例
// BoostStringAlgorithms.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//
#include <iostream>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
int main()
{
cout << "-------------------boost庫大小寫轉換------------------" << endl;
string tmpStrUpper = "HELLO,I'M ISMILELI";
string tmpStrLower = "hello,i'm ismileli";
cout << "-------------------打印原來字符串的值------------------" << endl;
cout << "tmpStrUpper:" << tmpStrUpper << endl;
cout << "tmpStrLower:" << tmpStrLower << endl;
cout << "-------------------使用*_copy轉換大小寫------------------" << endl;
string upperToLowerStr = boost::algorithm::to_lower_copy(tmpStrUpper);
string lowerToUpperStr = boost::algorithm::to_upper_copy(tmpStrLower);
cout << "tmpStrUpper:" << tmpStrUpper << endl;
cout << "tmpStrLower:" << tmpStrLower << endl;
cout << "upperToLowerStr:" << upperToLowerStr << endl;
cout << "lowerToUpperStr:" << lowerToUpperStr << endl;
cout << "-------------------使用不帶後綴*_copy轉換大小寫------------------" << endl;
boost::algorithm::to_lower(tmpStrUpper); // 沒有返回值
boost::algorithm::to_upper(tmpStrLower);
cout << "tmpStrUpper:" << tmpStrUpper << endl;
cout << "tmpStrLower:" << tmpStrLower << endl;
std::cout << "Hello World!\n";
getchar();
}
運行結果:
二、刪除
1、刪除函數
boost庫提供了衆多的字符串刪除函數,並且提供了很多版本供使用,例如以i開頭的用來區分大小寫敏感、以_copy結尾的以不改變原來的字符串等,以滿足使用者不同的需求。
boost庫提供的刪除函數如下:
erase_first:刪除在字符串中第一個出現的字符串。
erase_last:刪除在字符串中最後一個出現的字符串。
erase_nth:刪除在字符串中第n個出現的字符串。
erase_all:刪除在字符串中所有出現的字符串。
erase_head:刪除輸入的開頭。
erase_tail:刪除輸入的結尾。
2、刪除代碼示例
// BoostStringAlgorithms.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//
#include <iostream>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
int main()
{
cout << "-------------------boost庫字符串刪除------------------" << endl;
string tmpStrErase = "Hello!Hello!I'm ISmileLi!Hello!Hello!I'm ISmileLi!";
cout << "-------------------打印原來字符串的值------------------" << endl;
cout << "tmpStrErase:" << tmpStrErase << endl;
boost::algorithm::erase_first(tmpStrErase,"Hello");
cout << "-------------------刪除第一個字符串後------------------" << endl;
cout << "tmpStrErase:" << tmpStrErase << endl;
string tmpEraseLastStr = boost::algorithm::erase_last_copy(tmpStrErase, "Hello");
cout << "-------------------刪除最後一個字符串後------------------" << endl;
cout << "tmpStrErase:" << tmpStrErase << endl;
cout << "tmpEraseLastStr:" << tmpEraseLastStr << endl;
boost::algorithm::erase_nth(tmpStrErase, "Hello",2);
cout << "-------------------刪除第2個字符串後------------------" << endl;
cout << "tmpStrErase:" << tmpStrErase << endl;
boost::algorithm::erase_all(tmpStrErase, "Hello");
cout << "-------------------刪除所有出現的字符串後------------------" << endl;
cout << "tmpStrErase:" << tmpStrErase << endl;
string tmpEraseHeadCopyStr = boost::algorithm::erase_head_copy(tmpStrErase,5);
cout << "-------------------刪除開頭出現的幾個字符串後------------------" << endl;
cout << "tmpStrErase:" << tmpStrErase << endl;
cout << "tmpEraseHeadCopyStr:" << tmpEraseHeadCopyStr << endl;
std::cout << "Hello World!\n";
getchar();
}
運行結果:
三、替換
1、替換函數
boost庫提供了衆多的字符串替換函數,並且提供了很多版本供使用,例如以i開頭的用來區分大小寫敏感、以_copy結尾的以不改變原來的字符串等,以滿足使用者不同的需求。
boost庫提供的替換函數如下:
replace_first:替換在字符串中第一個出現的字符串。
replace_last:替換在字符串中最後一個出現的字符串。
replace_nth:替換在字符串中第n個出現的字符串。
replace_all:替換在字符串中所有出現的字符串。
replace_head:替換輸入的開頭。
replace_tail:替換輸入的結尾。
2、替換代碼示例
// BoostStringAlgorithms.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//
#include <iostream>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
int main()
{
cout << "-------------------boost庫字符串替換------------------" << endl;
string tmpStrReplace = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";
cout << "-------------------打印原來字符串的值------------------" << endl;
cout << "tmpStrReplace:" << tmpStrReplace << endl;
boost::algorithm::replace_first(tmpStrReplace, "HELLO","hello");
cout << "-------------------替換第一個字符串後------------------" << endl;
cout << "tmpStrReplace:" << tmpStrReplace << endl;
string tmpReplaceLastStr = boost::algorithm::replace_last_copy(tmpStrReplace, "HELLO","hello");
cout << "-------------------替換最後一個字符串後------------------" << endl;
cout << "tmpStrReplace:" << tmpStrReplace << endl;
cout << "tmpReplaceLastStr:" << tmpReplaceLastStr << endl;
boost::algorithm::replace_nth(tmpStrReplace, "HELLO", 2, "hello");
cout << "-------------------替換第2個字符串後------------------" << endl;
cout << "tmpStrReplace:" << tmpStrReplace << endl;
boost::algorithm::replace_all(tmpStrReplace, "HELLO", "hello");
cout << "-------------------替換所有出現的字符串後------------------" << endl;
cout << "tmpStrReplace:" << tmpStrReplace << endl;
string tmpReplaceTailCopyStr = boost::algorithm::replace_tail_copy(tmpStrReplace, 5, "hello");
cout << "-------------------替換結尾出現的幾個字符串後------------------" << endl;
cout << "tmpStrReplace:" << tmpStrReplace << endl;
cout << "tmpReplaceHeadCopyStr:" << tmpReplaceTailCopyStr << endl;
std::cout << "Hello World!\n";
getchar();
}
運行結果:
四、查找
1、查找函數
boost庫提供了衆多的字符串查找函數,並且提供了很多版本供使用,例如以i開頭的用來區分大小寫敏感的以不改變原來的字符串等,沒有_copy版本,以滿足使用者不同的需求。
boost庫提供的查找函數如下:
find_first:查找在字符串中第一個出現的字符串。
find_last:查找在字符串中最後一個出現的字符串。
find_nth:查找在字符串中第n個出現的字符串。
find_head:查找輸入的開頭第N個子串。
find_tail:查找輸入的結尾第N個子串。
2、查找代碼示例
// BoostStringAlgorithms.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//
#include <iostream>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
int main()
{
cout << "-------------------boost庫字符串查找------------------" << endl;
string tmpStrFind = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";
cout << "-------------------打印原來字符串的值------------------" << endl;
cout << "tmpStrFind:" << tmpStrFind << endl;
boost::iterator_range<string::iterator> rIter = boost::algorithm::find_first(tmpStrFind, "ISmileLi");
cout << "查找第一個字符串出現的位置: " << rIter.begin()-tmpStrFind.begin() << endl;
rIter = boost::algorithm::find_last(tmpStrFind, "ISmileLi");
cout << "查找最後一個字符串ISmileLi出現的位置: " << rIter.begin() - tmpStrFind.begin() << endl;
rIter = boost::algorithm::find_nth(tmpStrFind, "HELLO",3);
cout << "查找第3個字符串HELLO出現的位置: " << rIter.begin() - tmpStrFind.begin() << endl;
rIter = boost::algorithm::find_head(tmpStrFind, 3);
cout << "查找開頭3個字符串出現的位置: " << rIter.begin() - tmpStrFind.begin() << endl;
cout << "rIter:" << rIter << endl;
rIter = boost::algorithm::find_tail(tmpStrFind, 3);
cout << "查找結尾3個字符串出現的位置: " << rIter.begin() - tmpStrFind.begin() << endl;
cout << "rIter:" << rIter << endl;
std::cout << "Hello World!\n";
getchar();
}
運行結果:
五、判別式和分類
1、判別式和分類函數
boost庫提供了一組判別式函數用來判斷大小、等於關係,分類函數用來分類哪些是數字、哪些是字符、哪些是空格、哪些是十進制等等,檢測一個字符是否是某種特性。
判別式函數和分類函數大多數都是以is_開頭,這些函數如下:
判別式函數包括:is_equal(等於)、is_less(小於)、is_not_greater(不大於)、lexicographical_compare(根據順序檢測一個字符串是否小於另一個)、starts_with(檢測一個字符串是否是另一個的前綴)、ends_with(檢測一個字符串是否是另一個的後綴)、contains(包含)、equals(相等)、all(檢測一個字符串中的所有元素是否滿足指定的判別式)。
分類函數:is_space、is_alnum(字符和數字)、is_alpha(字母)、is_cntrl(控制符)、is_digit(十進制)、is_graph(圖形字符)、is_lower(小寫)、is_upper(大寫)、is_print(可打印字符)、is_punct(標點符號)、is_xdigit(十六進制)、is_any_of(是否是序列中的任意字符)、if_from_range(是否位於指定區間,包括兩頭)
2、判別式和分類代碼示例
使用比較簡單,不再單獨示例,可以看下一節的代碼示例
六、修剪
1、修剪函數
boost庫提供了衆多的字符串修剪函數,並且提供了很多版本供使用,例如以_copy、_if、_copy_if結尾的版本等,以滿足使用者不同的需求。
主要函數有trim_left、trim_right、trim這三種方式以及它們的變種版本。
2、修剪代碼示例
// BoostStringAlgorithms.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//
#include <iostream>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
int main()
{
cout << "-------------------boost庫字符串修剪------------------" << endl;
string tmpTrimSr = " ...88HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!666... ";
cout << "-------------------打印原來字符串的值------------------" << endl;
cout << "tmpTrimSr:" << tmpTrimSr << endl;
cout << "-------------------刪除字符串空格------------------" << endl;
string trimSpace = boost::algorithm::trim_copy(tmpTrimSr);
cout << "不改變原字符串:" << trimSpace << endl;
cout << "改變原字符串,刪除左邊:" << endl;
boost::trim_left(tmpTrimSr);
cout << tmpTrimSr << endl;
cout << "改變原字符串,刪除右邊:" << endl;
boost::trim_right(tmpTrimSr);
cout << tmpTrimSr << endl;
cout << "-------------------使用判別式刪除字符串兩端的空格、標點、數字------------------" << endl;
cout << boost::trim_copy_if(tmpTrimSr, is_space() || is_punct() || is_digit()) << endl;
std::cout << "Hello World!\n";
getchar();
}
運行結果:
七、分割
1、分割函數
boost庫提供了一個分割函數split(),可以根據某種特定的字符或者策略把分割後的字符,保存到指定的容器中。
2、分割代碼示例
// BoostStringAlgorithms.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//
#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
int main()
{
cout << "-------------------boost庫字符串分割------------------" << endl;
string tmpStrFind = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";
cout << "原字符串:" << tmpStrFind << endl;
vector<std::string> splitVector;
// 使用標點符號切割字符串
boost::algorithm::split(splitVector, tmpStrFind, boost::algorithm::is_punct());
cout << "-----------打印切割後的字符串----------" << endl;
for (int i=0; i<splitVector.size(); ++i)
{
cout << splitVector.at(i) << endl;
}
std::cout << "Hello World!\n";
getchar();
}
運行結果:
八、合併
1、合併函數
boost庫提供了一個合併函數join(),它可以把存儲在容器中的字符串通過指定的分隔符連接在一起。
2、合併代碼示例
// BoostStringAlgorithms.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//
#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
int main()
{
cout << "-------------------boost庫字符串合併------------------" << endl;
string tmpStrFind = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";
cout << "原字符串:" << tmpStrFind << endl;
vector<std::string> splitVector;
// 使用標點符號切割字符串
boost::algorithm::split(splitVector, tmpStrFind, boost::algorithm::is_punct());
cout << "-----------打印切割後的字符串----------" << endl;
for (int i = 0; i < splitVector.size(); ++i)
{
cout << splitVector.at(i) << endl;
}
cout << "-----------使用合併函數join並打印合並後的字符串----------" << endl;
cout << boost::algorithm::join(splitVector, "+") << endl;
string tempJoinIfStr = boost::algorithm::join_if(splitVector, "$$", [](string tmpStr){
return boost::algorithm::contains(tmpStr, "I");
});
cout << "tempJoinIfStr: " << tempJoinIfStr << endl;
std::cout << "Hello World!\n";
getchar();
}
運行結果:
九、題外話
1、boost提供的字符串操作還有很多,無法一一舉例列出,更多使用方法還需要自己多多研究一下。
2、更多boost庫信息請移步這裏:https://blog.csdn.net/toby54king/category_7762600.html
3、這篇文章寫了整整4個多小時,實在不容易啊,希望各位老鐵多多支持!
原創不易,點贊鼓勵一下吧!