Boost下字符串处理:大小写转换,判断和剪切

最近在做任务,其中涉及到字符串的处理,用到了boost库中的函数。现在总结,并结合实例记录一下。


1.大小写转换

string_t fileName("Cn.xunlei.eXe");	
//转换为大写
boost::algorithm::to_upper(fileName);//fileName = "CN.XUNLEI.EXE"
//转换为小写
boost::algorithm::to_lower(fileName);//fileName = "cn.xunlei.exe"


2.判断

string_t fileName("Cn.xunlei.eXe");

//是否以特定字符串结尾,是就返回true,否就返回false
bool ends_result0 = boost::algorithm::ends_with(fileName, ".eX");//ends_result0 = false
bool ends_result1 = boost::algorithm::ends_with(fileName, ".eXe");//ends_result1 = true

//是否以特定字符串开头,是就返回true,否就返回false
bool starts_result0 = boost::algorithm::starts_with(fileName, "Cnn.");//starts_result0 = false
bool starts_result1 = boost::algorithm::starts_with(fileName, "Cn.");//starts_result1 = true

//是否包含特定字符串,是就返回true,否就返回false
bool contains_result0 = boost::algorithm::contains(fileName, "tian");//contains_result0 = false
bool contains_result1 = boost::algorithm::contains(fileName, "xun");//contains_result1 = true

//是否等于特定字符串,是就返回true,否就返回false
bool equals_result0 = boost::algorithm::equals(fileName, "cn.tian.exe");//equals_result0 = false
bool equals_result1 = boost::algorithm::equals(fileName, "Cn.xunlei.eXe");//equals_result1 = true

//根据字典顺序检测一个字符串是否小于另一个字符串,是就返回true,否就返回false
bool compare_result0 = boost::algorithm::lexicographical_compare("b", "C");//compare_result0 = false
bool compare_result1 = boost::algorithm::lexicographical_compare("B", "a");//compare_result1 = true
bool compare_result2 = boost::algorithm::lexicographical_compare("bBCD", "CBCD");//compare_result2 = false
bool compare_result3 = boost::algorithm::lexicographical_compare("BBCD", "aBCD");//compare_result3 = true


3.剪切

string_t example_left("    hello world!    ");
//剪切掉开头的空格
boost::algorithm::trim_left(example_left);//example_left = "hello world!    "

string_t example_right("    hello world!    ");
//剪切掉结尾的空格
boost::algorithm::trim_right(example_right);//example_right = " hello world!"

string_t example_both("    hello world!    ");
//剪切掉开头和结尾的空格
boost::algorithm::trim(example_both);//example_both = "hello world!"


4.最后,有三个符号需要说明一下

1)i_:函数名中没有这个符号表示大小写敏感;有这个符号表示大小写不敏感
string_t fileName("Cn.xunlei.eXe");
bool ends_result0 = boost::algorithm::ends_with(fileName, ".exe");//ends_result0 = false
bool ends_result1 = boost::algorithm::ends_with(fileName, ".eXe");//ends_result1 = true
bool ends_result2 = boost::algorithm::iends_with(fileName, ".exe");//ends_result2 = true
bool ends_result3 = boost::algorithm::iends_with(fileName, ".eXe");//ends_result3 = true

2)_copy:函数名中没有这个符号表示直接修改源字符串; 有这个符号表示输入不变,返回处理后的拷贝;
string_t fileName("Cn.xunlei.eXe");
	
string_t flieName_copy = boost::algorithm::to_upper_copy(fileName);//fileName = "Cn.xunlei.eXe" ,flieName_copy = "CN.XUNLEI.EXE"
boost::algorithm::to_upper(fileName);//fileName = "CN.XUNLEI.EXE"


3)_if:函数名中没有这个符号表示使用默认的判断方法;有这个符号表示需要一个作为判断方法的函数对象;
string_t example("   :\"<>?/02\[]?,.?;'[]\{}|  hello world!,. 120`~!@#$%^&*()_+-=*   ");
//只删掉了开头和结尾的空格
boost::algorithm::trim(example);//example = ":"<>?/02[]?,.?;'[]{}|  hello world!,. 120`~!@#$%^&*()_+-=*"

string_t example_if("   :\"<>?/02\[]?,.?;'[]\{}|  hello world!,. 120`~!@#$%^&*()_+-=*   ");
//删掉了开头和结尾的空格、数字和标点符号
boost::algorithm::trim_if(example_if, boost::algorithm::is_any_of(" ") || boost::algorithm::is_digit() || boost::algorithm::is_punct());//example_if = "hello world"


你可能感兴趣的:(C++)