文章目錄
- 1.判斷
- 1.1.equals
- 1.2.all
- 1.3.starts_with
- 1.4.ends_with
- 1.5.contains
- 2.大小寫轉換
- 3.字符串刪除
- 4.字符串替換
- 5.字符串查找
- 6.字符串修剪
- 7.字符串分割
- 8.字符串合并
- 9.總結
1.判斷
判別式函數和分類函數大多數都是以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(是否位于指定區間,包括兩頭)
1.1.equals
#include <boost/algorithm/string.hpp>
assert(boost::equals("boost", "boost"));
assert(!boost::equals("boost", "BOOST"));
assert(boost::iequals("boost", "BOOST"));
1.2.all
all , 如果它的所有元素滿足一個給定的通過判斷式描述的條件,則這個條件式成立。
assert(boost::all("\x20\t\n\r", boost::is_space()));
assert(boost::all("\x20\t\n\r", boost::is_classified(std::ctype_base::space)));
assert(boost::all("\x20\t\n\r", boost::is_any_of("\x20\t\n\r")));
assert(boost::all("abcde", boost::is_from_range('a', 'e')));
assert(boost::all("abcde", boost::is_from_range('a', 'z')));
assert(!boost::all("abcde", boost::is_from_range('b', 'c')));
assert(boost::all("abc __ de", boost::is_from_range('a', 'z') || boost::is_space() || boost::is_any_of("_")));
1.3.starts_with
assert(boost::starts_with("boost_python-vc100-mt-1_49.dll", "boost"));
assert(!boost::starts_with("boost_python-vc100-mt-1_49.dll", "BOOST"));
assert(boost::istarts_with("boost_python-vc71-mt-1_33.dll", "BOOST"));
1.4.ends_with
assert(boost::ends_with("boost_python-vc100-mt-1_49.dll", ".dll"));
assert(!boost::ends_with("boost_python-vc100-mt-1_49.dll", ".DLL"));
assert(boost::iends_with("boost_python-vc100-mt-1_49.dll", ".DLL"));
1.5.contains
assert(boost::contains("boost_python-vc100-mt-1_49.dll", "python"));
assert(!boost::contains("boost_python-vc100-mt-1_49.dll", "PYTHON"));
assert(boost::icontains("boost_python-vc100-mt-1_49.dll", "PYTHON"));
is_space: 字符是否為空格
is_alnum: 字符是否為字母和數字字符
is_alpha: 字符是否為字母
is_cntrl: 字符是否為控制字符
is_digit: 字符是否為十進制數字
is_graph: 字符是否為圖形字符
is_lower: 字符是否為小寫字符
is_print: 字符是否為可打印字符
is_punct: 字符是否為標點符號字符
is_upper: 字符是否為大寫字符
is_xdigit: 字符是否為十六進制數字
is_any_of: 字符是否是參數字符序列中的任意字符
is_from_range 字符是否位于指定區間內,即from <= ch <= to
2.大小寫轉換
主要有如下API:
to_lower_copy:將原來字符串,轉換為小寫字符串,并返回新的字符串,原來字符串不改變。
to_upper_copy:將原來字符串,轉換為大寫字符串,并返回新的字符串,原來字符串不改變。
to_lower:將原來字符串,轉換為小寫字符串,原來字符串改變。
to_upper:將原來字符串,轉換為大寫字符串,原來字符串改變。
3.字符串刪除
boost庫提供了眾多的字符串刪除函數,并且提供了很多版本供使用,例如以i開頭的用來區分大小寫敏感、以_copy結尾的以不改變原來的字符串等,以滿足使用者不同的需求。
erase_first:刪除在字符串中第一個出現的字符串。
erase_last:刪除在字符串中最后一個出現的字符串。
erase_nth:刪除在字符串中第n個出現的字符串。
erase_all:刪除在字符串中所有出現的字符串。
erase_head:刪除輸入的開頭。
erase_tail:刪除輸入的結尾。
#include <iostream>
#include <boost/algorithm/string.hpp>int main()
{std::string tmpStrErase = "Hello!Hello!I'm ISmileLi!Hello!Hello!I'm ISmileLi!";std::cout << "tmpStrErase:" << tmpStrErase << std::endl;boost::algorithm::erase_first(tmpStrErase, "Hello");std::cout << "tmpStrErase:" << tmpStrErase << std::endl;std::string tmpEraseLastStr = boost::algorithm::erase_last_copy(tmpStrErase, "Hello");std::cout << "tmpStrErase:" << tmpStrErase << std::endl;std::cout << "tmpEraseLastStr:" << tmpEraseLastStr << std::endl;boost::algorithm::erase_nth(tmpStrErase, "Hello", 2);std::cout << "tmpStrErase:" << tmpStrErase << std::endl;boost::algorithm::erase_all(tmpStrErase, "Hello");std::cout << "tmpStrErase:" << tmpStrErase << std::endl;std::string tmpEraseHeadCopyStr = boost::algorithm::erase_head_copy(tmpStrErase, 5);std::cout << "tmpStrErase:" << tmpStrErase << std::endl;std::cout << "tmpEraseHeadCopyStr:" << tmpEraseHeadCopyStr << std::endl;std::cout << "Hello World!\n";getchar();
}
4.字符串替換
boost庫提供了眾多的字符串替換函數,并且提供了很多版本供使用,例如以i開頭的用來區分大小寫敏感、以_copy結尾的以不改變原來的字符串等,以滿足使用者不同的需求。
boost庫提供的替換函數如下:
replace_first:替換在字符串中第一個出現的字符串。
replace_last:替換在字符串中最后一個出現的字符串。
replace_nth:替換在字符串中第n個出現的字符串。
replace_all:替換在字符串中所有出現的字符串。
replace_head:替換輸入的開頭。
replace_tail:替換輸入的結尾。
#include <iostream>
#include <boost/algorithm/string.hpp>int main()
{std::cout << "-------------------boost庫字符串替換------------------" << std::endl;std::string tmpStrReplace = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";std::cout << "-------------------打印原來字符串的值------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;boost::algorithm::replace_first(tmpStrReplace, "HELLO", "hello");std::cout << "-------------------替換第一個字符串后------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;std::string tmpReplaceLastStr = boost::algorithm::replace_last_copy(tmpStrReplace, "HELLO", "hello");std::cout << "-------------------替換最后一個字符串后------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;std::cout << "tmpReplaceLastStr:" << tmpReplaceLastStr << std::endl;boost::algorithm::replace_nth(tmpStrReplace, "HELLO", 2, "hello");std::cout << "-------------------替換第2個字符串后------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;boost::algorithm::replace_all(tmpStrReplace, "HELLO", "hello");std::cout << "-------------------替換所有出現的字符串后------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;std::string tmpReplaceTailCopyStr = boost::algorithm::replace_tail_copy(tmpStrReplace, 5, "hello");std::cout << "-------------------替換結尾出現的幾個字符串后------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;std::cout << "tmpReplaceHeadCopyStr:" << tmpReplaceTailCopyStr << std::endl;std::cout << "Hello World!\n";getchar();
}
5.字符串查找
boost庫提供了眾多的字符串查找函數,并且提供了很多版本供使用,例如以i開頭的用來區分大小寫敏感的以不改變原來的字符串等,沒有_copy版本,以滿足使用者不同的需求。
boost庫提供的查找函數如下:
find_first:查找在字符串中第一個出現的字符串。
find_last:查找在字符串中最后一個出現的字符串。
find_nth:查找在字符串中第n個出現的字符串。
find_head:查找輸入的開頭第N個子串。
find_tail:查找輸入的結尾第N個子串。
#include <iostream>
#include <boost/algorithm/string.hpp>int main()
{std::cout << "-------------------boost庫字符串查找------------------" << std::endl;std::string tmpStrFind = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";std::cout << "-------------------打印原來字符串的值------------------" << std::endl;std::cout << "tmpStrFind:" << tmpStrFind << std::endl;boost::iterator_range<std::string::iterator> rIter = boost::algorithm::find_first(tmpStrFind, "ISmileLi");std::cout << "查找第一個字符串出現的位置: " << rIter.begin() - tmpStrFind.begin() << std::endl;//迭代器計算位置rIter = boost::algorithm::find_last(tmpStrFind, "ISmileLi");std::cout << "查找最后一個字符串ISmileLi出現的位置: " << rIter.begin() - tmpStrFind.begin() << std::endl;rIter = boost::algorithm::find_nth(tmpStrFind, "HELLO", 3);std::cout << "查找第3個字符串HELLO出現的位置: " << rIter.begin() - tmpStrFind.begin() << std::endl;rIter = boost::algorithm::find_head(tmpStrFind, 3);std::cout << "查找開頭3個字符串出現的位置: " << rIter.begin() - tmpStrFind.begin() << std::endl;std::cout << "rIter:" << rIter << std::endl;rIter = boost::algorithm::find_tail(tmpStrFind, 3);std::cout << "查找結尾3個字符串出現的位置: " << rIter.begin() - tmpStrFind.begin() << std::endl;std::cout << "rIter:" << rIter << std::endl;std::cout << "Hello World!\n";getchar();
}
6.字符串修剪
boost庫提供了眾多的字符串修剪函數,并且提供了很多版本供使用,例如以_copy、_if、_copy_if結尾的版本等,以滿足使用者不同的需求。主要函數有trim_left、trim_right、trim這三種方式以及它們的變種版本。
#include <iostream>
#include <boost/algorithm/string.hpp>int main()
{std::cout << "-------------------boost庫字符串修剪------------------" << std::endl;std::string tmpTrimSr = " ...88HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!666... ";std::cout << "-------------------打印原來字符串的值------------------" << std::endl;std::cout << "tmpTrimSr:" << tmpTrimSr << std::endl;std::cout << "-------------------刪除字符串空格------------------" << std::endl;std::string trimSpace = boost::algorithm::trim_copy(tmpTrimSr);std::cout << "不改變原字符串:" << trimSpace << std::endl;std::cout << "改變原字符串,刪除左邊:" << std::endl;boost::trim_left(tmpTrimSr);std::cout << tmpTrimSr << std::endl;std::cout << "改變原字符串,刪除右邊:" << std::endl;boost::trim_right(tmpTrimSr);std::cout << tmpTrimSr << std::endl;std::cout << "-------------------使用判別式刪除字符串兩端的空格、標點、數字------------------" << std::endl;std::cout << boost::trim_copy_if(tmpTrimSr, boost::is_space() || boost::is_punct() || boost::is_digit()) << std::endl;std::cout << "Hello World!\n";getchar();
}
7.字符串分割
boost庫提供了一個分割函數split(),可以根據某種特定的字符或者策略把分割后的字符,保存到指定的容器中。
#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>int main()
{std::cout << "-------------------boost庫字符串分割------------------" << std::endl;std::string tmpStrFind = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";std::cout << "原字符串:" << tmpStrFind << std::endl;std::vector<std::string> splitVector;// 使用標點符號切割字符串boost::algorithm::split(splitVector, tmpStrFind, boost::algorithm::is_punct());std::cout << "-----------打印切割后的字符串----------" << std::endl;for (int i = 0; i < splitVector.size(); ++i){std::cout << splitVector.at(i) << std::endl;}// 常規用法std::string str = "Hello,World,How,Are,You";std::vector<std::string> result;boost::split(result, str, boost::is_any_of(","));std::cout << "Hello World!\n";getchar();
}
8.字符串合并
boost庫提供了一個合并函數join(),它可以把存儲在容器中的字符串通過指定的分隔符連接在一起。
#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>int main()
{std::cout << "-------------------boost庫字符串合并------------------" << std::endl;std::string tmpStrFind = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";std::cout << "原字符串:" << tmpStrFind << std::endl;std::vector<std::string> splitVector;// 使用標點符號切割字符串boost::algorithm::split(splitVector, tmpStrFind, boost::algorithm::is_punct());std::cout << "-----------打印切割后的字符串----------" << std::endl;for (int i = 0; i < splitVector.size(); ++i){std::cout << splitVector.at(i) << std::endl;}std::cout << "-----------使用合并函數join并打印合并后的字符串----------" << std::endl;std::cout << boost::algorithm::join(splitVector, "+") << std::endl;//lambda表達式std::string tempJoinIfStr = boost::algorithm::join_if(splitVector, "$$", [](std::string tmpStr) {return boost::algorithm::contains(tmpStr, "I");});std::cout << "tempJoinIfStr: " << tempJoinIfStr << std::endl;std::cout << "Hello World!\n";getchar();
}
9.總結
字符串的常用操作,軟件工程師使用起來,還是非常方便快捷。