基本概念这里不解释了,代码中详细解释了BOOST程序库中对于字符串每一个方法的详细用法:
注意:这里每实践一个方法,都将上面实践过的方法进行了注释,如果全部取消注释,会出现重命名的问题。
c
#include <iostream>
#include <string>
//boost类型转换头文件
#include <boost/lexical_cast.hpp>
//boost格式化字符头文件
#include <boost/format.hpp>
//boost轻量化字符串头文件
#include <boost/utility/string_ref.hpp>
//字符串算法头文件
#include <boost/algorithm/string.hpp>
int main() {
/*
//C++自带的字符串转换:
int ivalue = std::stoi("42");
int lvalue = std::stol("122");
long long llvalue = std::stoll("42222");
double dbvalue = std::stod("42.222");
std::string szStr = std::to_string(12138);
std::cout << typeid(ivalue).name() << "\t" << typeid(lvalue).name() << "\t" << typeid(llvalue).name() << "\t" << typeid(dbvalue).name() << "\t" << typeid(szStr).name() << std::endl;
std::cout << ivalue << "\t" << lvalue << "\t" << llvalue << "\t" << dbvalue << "\t" << szStr << std::endl;
//输出:int int __int64 double class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
//42 122 42222 42.222 12138
//boost程序库的转换:
int nValue = boost::lexical_cast<int> ("123");
long lValue = boost::lexical_cast<long>("123456");
long long llValue = boost::lexical_cast<long long>("123456789");
double dbValue = boost::lexical_cast<double> ("12.123456");
bool bValue = boost::lexical_cast<bool> ("1");
std::string Str = boost::lexical_cast<std::string> (123456789);
std::cout << typeid(nValue).name() << "\t" << typeid(lValue).name() << "\t" << typeid(llValue).name() << "\t" << typeid(dbValue).name() << "\t" << typeid(bValue).name() << "\t" << typeid(Str).name() << std::endl;
std::cout << nValue << "\t" << lValue << "\t" << llValue << "\t" << dbValue << "\t" << bValue << "\t" << Str << std::endl;
//输出:int long __int64 double class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
//123 123456 123456789 42.222 123456789
//使用bosot转换的时候,如果转化失败,会抛出异常:
try {
int nValue = boost::lexical_cast<int> ("123");
long lValue = boost::lexical_cast<long>("123456");
long long llValue = boost::lexical_cast<long long>("123456789");
double dbValue = boost::lexical_cast<double> ("12.123456");
bool bValue = boost::lexical_cast<bool> ("123");
std::string Str = boost::lexical_cast<std::string> (123456789);
std::cout << typeid(nValue).name() << "\t" << typeid(lValue).name() << "\t" << typeid(llValue).name() << "\t" << typeid(dbValue).name() << "\t" << typeid(bValue).name() << "\t" << typeid(Str).name() << std::endl;
std::cout << nValue << "\t" << lValue << "\t" << llValue << "\t" << dbValue << "\t" << bValue << "\t" << Str << std::endl;
}
catch (boost::bad_lexical_cast &e) {
std::cout << "error" << "\t" << e.what() << std::endl;
}
//输出:error bad lexical cast: source type value could not be interpreted as target
//也可以使用不抛出异常的方式,使用返回的bool来判断是否转换成功:
int nNum;
boost::conversion::try_lexical_convert(123456, nNum);
std::cout << nNum << std::endl;
boost::format fmt("%s-%d-%d-%d");
std::cout << fmt % "fdjskalf;djska" % 2023 % 05 % 24<<std::endl;
//输出:fdjskalf;djska-2023-5-24
//也可以使用匿名对象的方式:
std::cout << boost::format("%s-%d-%d-%d") % "format" % 2023 % 05 % 31 << std::endl;
//输出:format-2023-5-31
//实际上,底层进行了二次封装:basic_format
boost::basic_format<char> bfmt("%s-%d-%d-%d");
std::cout << bfmt % "bfmit" % 222 % 111 % 000 << std::endl;
//输出:bfmit-222-111-0
//boost中的轻量化字符串:
//C++本身在进行字符串的操作的时候,会有一定的成本问题:比如说:
const char* str = "This is a string";
std::string Str(str);
//在这个过程中,存在一次string类的深拷贝过程
//使用boost轻量化字符串:也可以使用=赋值的方法(注意这里是字符串的引用,所以不能修改原值
boost::string_ref S1(str);
boost::string_ref S2(Str);
std::cout << S1 << "\t" << S2 << std::endl; //输出:This is a string This is a string
//可以指定字符个数:
boost::string_ref S3(str, 4);
std::cout << S3 << std::endl; //输出:This
//字符串的判断:
if (S3==S1.substr(0,4)) {
std::cout << "==" << std::endl;
}
//可以在任意位置取出字符:
std::cout << S1.front() << std::endl; //T
std::cout << S1.at(2) << std::endl; //i
std::cout << S1.back() << std::endl; //g
//含有迭代器:
for (auto i = S1.begin(); i != S1.end(); i++) {
std::cout << *i;
}//This is a string
std::cout << std::endl;
//字符串的比对:这个方法,如同strcmp wcscmp返回0代表比对成功
std::cout << S1.compare(S2) << std::endl;
//可以取出原始字符串:
const char* p = S1.data();
std::cout << p << std::endl; //This is a string
//查找是否存在字串:
auto a = S1.find("is");
std::cout << typeid(S1.find("is")).name() << std::endl; //unsigned int
std::cout << a << std::endl; //返回第一次出现的位置
//对字符串的判断/等操作,头文件:boost/algorithm/string.hpp
std::string Str = "readmi.txt";
//判断文件格式,也就是判断字符串尾:
if (boost::ends_with(Str, ".txt")) {
std::cout << "txt文件" << std::endl;
}
//判断头部:
if (boost::starts_with(Str, "rea")) {
std::cout << "rea" << std::endl;
}
//判断一个字符是否包含另一个字符串:
if (boost::contains(Str, "read")) {
std::cout << "contains" << std::endl;
}
//对字符串进行大小写转换:
boost::to_upper(Str);
std::cout << Str << std::endl; //READMI.TXT
boost::to_lower(Str);
std::cout << Str << std::endl; //readmi.txt
//替换字符:replace_first:替换第一次出现的/replace_last:替换最后一次出现的/replace_all:替换所有
std::string Str = "readmeread.txt";
//boost::replace_first(Str, "read", "MMMM");
//std::cout << Str << std::endl; //MMMMmeread.txt
//boost::replace_last(Str, "read", "MMMM");
//std::cout << Str << std::endl; //readmeMMMM.txt
boost::replace_all(Str, "read", "MMMM");
std::cout << Str << std::endl; //MMMMmeMMMM.txt
//空格删除:
//1. 删除所有空格:
std::string Str = " abcde ";
//boost::trim(Str);
std::cout << Str << std::endl; //abcde
//2. 删除左边的空格:
//boost::trim_left(Str); //abcde a
std::cout << Str << "a" << std::endl;
//3. 删除右边的空格:
boost::trim_right(Str);
std::cout << Str << "a" << std::endl; //abcdea
*/
//字符串的查找:
std::string Str = "readreadreadmereadreadread.txt";
std::cout << typeid(boost::find_first(Str, "read")).name() << std::endl; //返回值类型:class boost::iterator_range<class std::_String_iterator<class std::_String_val<struct std::_Simple_types<char> > >
auto a = boost::find_first(Str, "read");
std::cout << a << std::endl; //read
//boost::find_last方法:取出最后出现的
//从字符串中取出字符:
//取出头部6字节:
auto head = boost::find_head(Str, 6);
std::cout << typeid(boost::find_head(Str, 6)).name() << std::endl; //class boost::iterator_range<class std::_String_iterator<class std::_String_val<struct std::_Simple_types<char> > > >
std::cout << head << std::endl; //readre
//取出尾部6字节:
auto tail = boost::find_tail(Str, 6);
std::cout << typeid(boost::find_tail(Str, 6)).name() << std::endl; //class boost::iterator_range<class std::_String_iterator<class std::_String_val<struct std::_Simple_types<char> > > >
std::cout << tail << std::endl; //ad.txt
return 0;
}
如果发现文章中有错误,还请大家指出来,我会非常虚心地学习,我们一起进步!!!