boost库:字符串处理
使⽤boost库的字符串处理之前,需要进⾏区域设置。类:std::locale,每个C++程序⾃动拥有⼀个此类的实例,不能直接访问全局区域设置。
全局区域设置可以使⽤类std::locale中的静态函数global()改变。
#include <locale>
#include <iostream>
int main() {
std::locale::global(std::locale("German"));
std::locale loc;
std::cout << loc.name() << std::endl;
return0;
}
静态函数global()接受⼀个类型为std::locale的对象作为其唯⼀的参数。
正则表达式库 Boost.Regex
boost::regex ⽤于定义⼀个正则表达式库
boost::smatch可以保存搜索结果
#include <boost/regex.hpp>
#include <locale>
#include <iostream>
int main() {
std::locale::global(std::locale("German"));
std::string s = "Boris Schaling";
boost::regex expr("\\w+\\s\\w+");
 std::cout << boost::regex_match(s, expr) << std::endl;
return 0;
}
boost::regex_match()⽤于字符串与正则表达式的⽐较,字符串匹配正则表达式时返回true。
下⾯介绍⼀下boost string常⽤的算法接⼝:
1. case conversiion
to_upper()  将输⼊字符串转换成⼤写。
to_upper_copy() 输⼊字符串不变,返回值为转换成⼤写的字符串。
to_lower()  将输⼊字符串转换成⼩写。
to_lower_copy()  输⼊字符串不变,返回值为转换成⼩写的字符串。
2. trimming
trim_left() 将输⼊字符串左边的空格删除。
trim_left_copy() 输⼊字符串不变,返回去除左边空格的字符串。
trim_left_if() 将输⼊字符串左边符合条件的字符去除。trim_left_if("12hello", is_digit()), 输出 hello
trim_left_copy_if() 输⼊字符串不变,意思同上。
trim_right() 意思同left差不多。删除右边的空格
trim_right_if()
trim_right_copy()
trim_right_copy_if()
trim() 删除⾸尾两端的空格。
trim_if()
trim_copy()
trim_copy_if()
3. predicates
bool starts_with(input, test) 判断input是否以test为开端。
bool istarts_with(input, test) 判断input是否以test为开端,⼤⼩写不敏感。
bool ends_with(input, test) 判断input是否以test结尾。
bool iends_with(input, test) 判断input是否以test结尾,⼤⼩写不敏感。
bool contains(input, test) 判断test是否在input⾥。
bool icontains(input, test) 判断test是否在input⾥,⼤⼩写不敏感。
bool equals(input, test) 判断input和test是否相等。
bool iequals(input, test) 判断input和test是否相等,⼤⼩写不敏感。
bool lexicographical_compare() 按字典顺序检测input1是否⼩于input2。
trim函数用于删除空格
bool ilexicographical_compare() 按字典顺序检测input1是否⼩于input2,⼤⼩写不敏感。
bool all() 检测输⼊的每个字符是否符合给定的条件。
4. find algorithms
iterator_range<string::iterator> find_first(input, search) 从input中查第⼀次出现search的位置。
iterator_range<string::iterator> ifind_first(input, search) 从input中查第⼀次出现search的位置,⼤⼩写不敏感。
iterator_range<string::iterator> find_last(input, search) 从input中查最后⼀次出现search的位置。
iterator_range<string::iterator> ifind_last(input, search) 从input中查最后⼀次出现search的位置,⼤⼩写不敏感。iterator_range<string::iterator> find_nth(input, search, n) 从input中查第n次(n从0开始)出现search的位置。
iterator_range<string::iterator> ifind_nth(input, search, n) 从input中查第n次(n从0开始)出现search的位置,⼤⼩写不敏感。iterator_range<string::iterator> find_head(input, n) 获取input开头n个字符的字串。
iterator_range<string::iterator> find_tail(input, n) 获取input尾部n个字符的字串。
iterator_range<string::iterator> find_token()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。