注:如果还有string方面的需求,告诉我,我加上去
[功能] stl的string的功能扩展,继承了string。主要包括trim(去掉字符串头尾的空格、制表符、回车等),字符串与数字之间的转换,变为大小写等,因为是继承了string,所以string的全部功能都可以使用。
[头文件] “lib/xstring.h”
[作者] 谢欣
[编译时需包含]Rules.make中的$(OBJS)
[详细说明]
xstring & trim ():去掉字符串头尾的空格、制表符、回车等(注意,修改字符串本身的值)
xstring & ltrim ():去掉字符串头的空格、制表符、回车等(注意,修改字符串本身的值)
xstring & rtrim ():去掉字符串尾的空格、制表符、回车等(注意,修改字符串本身的值)
xstring & tolower ():把字符串全变为小写字母(注意,修改字符串本身的值)
xstring & toupper ():把字符串全变为大写字母(注意,修改字符串本身的值)
bool isnumber (void) const:判断这个字符串是不是全是由数字构成
int toint(void):把字符串转换为int
long tolong (void) 把字符串转换为long
long long tolonglong (void) 把字符串转换为long long
xstring & operator= (string &s):把普通string赋值给xstring。
以下是静态方法。
template<class T> string parse (T t):把任意类型转换为字符串,比如可以把int, long等数字转换为字符串,比如可以执行xstring::pasrse(3.5)把3.5转换为字符串”3.5”。
int str2int (const string &str) 把str转换为int
long str2long (const string &str) 把str转换为 long long
long long str2longlong (const string &str) 把str转换为long long
[详细文档] http://162.105.80.155/classxstring.html
[使用举例]
#include "lib/xstring.h"
int main()
{
cout<<"Test Constructor..."<<endl;
xstring xs("500");
std::cout<<"Call function of base class /"string/"--size() "<<xs.size()<<std::endl;
std::cout<<"Just direct output use cout<<: "<<xs<<std::endl;
xstring xs2;
xs2 = "old string";
std::cout<<"Try xs2 = /"old string/": "<<xs2<<std::endl;
}
{
cout<<endl<<"Test trim..."<<endl;
xstring x(" leading_space");
cout<<"The string after trim is /""<<x.trim()<<"/"."<<endl;
xstring x2(" leading_space");
x2.trim();
cout<<"The string after trim is /""<<x2<<"/"."<<endl;
xstring x3("trailing_space ");
cout<<"The string after trim is /""<<x3.trim()<<"/"."<<endl;
xstring x4("trailing_space ");
x4.trim();
cout<<"The string after trim is /""<<x4<<"/"."<<endl;
xstring x_null("");
cout<<"The /"/" after trim is /""<<x_null.trim()<<"/"."<<endl;
xstring x_space(" ");
cout<<"The /" /" after trim is /""<<x_space.trim()<<"/"."<<endl;
xstring x_space2(" ");
cout<<"The /" /" after trim is /""<<x_space2.trim()<<"/"."<<endl;
xstring x_table(" begin and end are table ");
cout<<"The xstring after trim is /""<<x_table.trim()<<"/"."<<endl;
xstring x_enter("/nbegin and end are //n./n");
cout<<"The xstring after trim is /""<<x_enter.trim()<<"/"."<<endl;
xstring mass(" /n mass massing /n /r ");
cout<<"The xstring after trim is /""<<mass.trim()<<"/"."<<endl;
}
{
cout<<endl<<"Test ltrim..."<<endl;
xstring x(" leading_space");
cout<<"The string after trim is /""<<x.ltrim()<<"/"."<<endl;
xstring x2(" leading_space");
x2.ltrim();
cout<<"The string after ltrim is /""<<x2<<"/"."<<endl;
xstring x3("trailing_space ");
cout<<"The string after ltrim is /""<<x3.ltrim()<<"/"."<<endl;
xstring x4("trailing_space ");
x4.ltrim();
cout<<"The string after ltrim is /""<<x4<<"/"."<<endl;
xstring x_null("");
cout<<"The /"/" after ltrim is /""<<x_null.ltrim()<<"/"."<<endl;
xstring x_space(" ");
cout<<"The /" /" after ltrim is /""<<x_space.ltrim()<<"/"."<<endl;
xstring x_space2(" ");
cout<<"The /" /" after ltrim is /""<<x_space2.ltrim()<<"/"."<<endl;
xstring x_table(" begin and end are table ");
cout<<"The xstring after ltrim is /""<<x_table.ltrim()<<"/"."<<endl;
xstring x_enter("/nbegin and end are //n./n");
cout<<"The xstring after ltrim is /""<<x_enter.ltrim()<<"/"."<<endl;
xstring mass(" /n mass massing /n /r ");
cout<<"The xstring after ltrim is /""<<mass.ltrim()<<"/"."<<endl;
}
{
cout<<endl<<"Test rtrim..."<<endl;
xstring x(" leading_space");
cout<<"The string after trim is /""<<x.rtrim()<<"/"."<<endl;
xstring x2(" leading_space");
x2.rtrim();
cout<<"The string after rtrim is /""<<x2<<"/"."<<endl;
xstring x3("trailing_space ");
cout<<"The string after rtrim is /""<<x3.rtrim()<<"/"."<<endl;
xstring x4("trailing_space ");
x4.rtrim();
cout<<"The string after rtrim is /""<<x4<<"/"."<<endl;
xstring x_null("");
cout<<"The /"/" after rtrim is /""<<x_null.rtrim()<<"/"."<<endl;
xstring x_space(" ");
cout<<"The /" /" after rtrim is /""<<x_space.rtrim()<<"/"."<<endl;
xstring x_space2(" ");
cout<<"The /" /" after rtrim is /""<<x_space2.rtrim()<<"/"."<<endl;
xstring x_table(" begin and end are table ");
cout<<"The xstring after rtrim is /""<<x_table.rtrim()<<"/"."<<endl;
xstring x_enter("/nbegin and end are //n./n");
cout<<"The xstring after rtrim is /""<<x_enter.rtrim()<<"/"."<<endl;
xstring mass(" /n mass massing /n /r ");
cout<<"The xstring after rtrim is /""<<mass.rtrim()<<"/"."<<endl;
}
std::cout<<xstring::parse(50)<<std::endl;
xstring x_lower("asd asdf a634sd fjslk&^$%#//|uihkjjk uiy jg ");
cout<<"Lower: "<<x_lower.tolower()<<endl;
cout<<"Upper: "<<x_lower.toupper()<<endl;
xstring x_mash("aWd F asf a634sd fjslk&^$%#|uiGehguihkjjk uiy jg ");
x_mash.toupper();
cout<<"Upper: "<<x_mash<<endl;
x_mash.tolower();
cout<<"Lower: "<<x_mash<<endl;
string str("abc");
xstring x23;
x23=str;
cout<<"x is "<<x23<<endl;
cout<<"Test str2int()"<<endl;
try {
cout<<"123: "<<xstring::str2int("123")<<endl;
cout<<"1: "<<xstring::str2int("1")<<endl;
cout<<"0: "<<xstring::str2int("0")<<endl;
cout<<"1234567890: "<<xstring::str2int("1234567890")<<endl;
cout<<"-1234567890: "<<xstring::str2int("-1234567890")<<endl;
cout<<"000999: "<<xstring::str2int("000999")<<endl;
cout<<"1A90: "<<xstring::str2int("1A90")<<endl;
} catch (string s) {
cout<<"Catch exception, it's "<<s<<endl;
}
cout<<endl<<"Test isnumber"<<endl;
xstring num1("1234");
cout<<num1<<": "<<num1.isnumber()<<endl;
xstring num2("-3587");
cout<<num2<<": "<<num2.isnumber()<<endl;
xstring num3("3a587");
cout<<num3<<": "<<num3.isnumber()<<endl;
cout<<endl<<"Test tolong"<<endl;
xstring num11("1234");
cout<<num11<<": "<<num11.tolong()<<endl;
xstring num22("-123456781");
cout<<num22<<": "<<num22.tolong()<<endl;
/*
xstring x24;
x24="1234567890";
cout<<"StrToInt():"<<x24<<"->"<<x24.StrToInt(10)<<endl;
x24="ABCDEF";
cout<<"StrToInt():"<<x24<<"->"<<x24.StrToInt(16)<<endl;
*/
cout<<"Test end."<<endl;
return 0;
}
[功能] stl的string的功能扩展,继承了string。主要包括trim(去掉字符串头尾的空格、制表符、回车等),字符串与数字之间的转换,变为大小写等,因为是继承了string,所以string的全部功能都可以使用。
[头文件] “lib/xstring.h”
[作者] 谢欣
[编译时需包含]Rules.make中的$(OBJS)
[详细说明]
xstring & trim ():去掉字符串头尾的空格、制表符、回车等(注意,修改字符串本身的值)
xstring & ltrim ():去掉字符串头的空格、制表符、回车等(注意,修改字符串本身的值)
xstring & rtrim ():去掉字符串尾的空格、制表符、回车等(注意,修改字符串本身的值)
xstring & tolower ():把字符串全变为小写字母(注意,修改字符串本身的值)
xstring & toupper ():把字符串全变为大写字母(注意,修改字符串本身的值)
bool isnumber (void) const:判断这个字符串是不是全是由数字构成
int toint(void):把字符串转换为int
long tolong (void) 把字符串转换为long
long long tolonglong (void) 把字符串转换为long long
xstring & operator= (string &s):把普通string赋值给xstring。
以下是静态方法。
template<class T> string parse (T t):把任意类型转换为字符串,比如可以把int, long等数字转换为字符串,比如可以执行xstring::pasrse(3.5)把3.5转换为字符串”3.5”。
int str2int (const string &str) 把str转换为int
long str2long (const string &str) 把str转换为 long long
long long str2longlong (const string &str) 把str转换为long long
[详细文档] http://162.105.80.155/classxstring.html
[使用举例]
#include "lib/xstring.h"
int main()
{
cout<<"Test Constructor..."<<endl;
xstring xs("500");
std::cout<<"Call function of base class /"string/"--size() "<<xs.size()<<std::endl;
std::cout<<"Just direct output use cout<<: "<<xs<<std::endl;
xstring xs2;
xs2 = "old string";
std::cout<<"Try xs2 = /"old string/": "<<xs2<<std::endl;
}
{
cout<<endl<<"Test trim..."<<endl;
xstring x(" leading_space");
cout<<"The string after trim is /""<<x.trim()<<"/"."<<endl;
xstring x2(" leading_space");
x2.trim();
cout<<"The string after trim is /""<<x2<<"/"."<<endl;
xstring x3("trailing_space ");
cout<<"The string after trim is /""<<x3.trim()<<"/"."<<endl;
xstring x4("trailing_space ");
x4.trim();
cout<<"The string after trim is /""<<x4<<"/"."<<endl;
xstring x_null("");
cout<<"The /"/" after trim is /""<<x_null.trim()<<"/"."<<endl;
xstring x_space(" ");
cout<<"The /" /" after trim is /""<<x_space.trim()<<"/"."<<endl;
xstring x_space2(" ");
cout<<"The /" /" after trim is /""<<x_space2.trim()<<"/"."<<endl;
xstring x_table(" begin and end are table ");
cout<<"The xstring after trim is /""<<x_table.trim()<<"/"."<<endl;
xstring x_enter("/nbegin and end are //n./n");
cout<<"The xstring after trim is /""<<x_enter.trim()<<"/"."<<endl;
xstring mass(" /n mass massing /n /r ");
cout<<"The xstring after trim is /""<<mass.trim()<<"/"."<<endl;
}
{
cout<<endl<<"Test ltrim..."<<endl;
xstring x(" leading_space");
cout<<"The string after trim is /""<<x.ltrim()<<"/"."<<endl;
xstring x2(" leading_space");
x2.ltrim();
cout<<"The string after ltrim is /""<<x2<<"/"."<<endl;
xstring x3("trailing_space ");
cout<<"The string after ltrim is /""<<x3.ltrim()<<"/"."<<endl;
xstring x4("trailing_space ");
x4.ltrim();
cout<<"The string after ltrim is /""<<x4<<"/"."<<endl;
xstring x_null("");
cout<<"The /"/" after ltrim is /""<<x_null.ltrim()<<"/"."<<endl;
xstring x_space(" ");
cout<<"The /" /" after ltrim is /""<<x_space.ltrim()<<"/"."<<endl;
xstring x_space2(" ");
cout<<"The /" /" after ltrim is /""<<x_space2.ltrim()<<"/"."<<endl;
xstring x_table(" begin and end are table ");
cout<<"The xstring after ltrim is /""<<x_table.ltrim()<<"/"."<<endl;
xstring x_enter("/nbegin and end are //n./n");
cout<<"The xstring after ltrim is /""<<x_enter.ltrim()<<"/"."<<endl;
xstring mass(" /n mass massing /n /r ");
cout<<"The xstring after ltrim is /""<<mass.ltrim()<<"/"."<<endl;
}
{
cout<<endl<<"Test rtrim..."<<endl;
xstring x(" leading_space");
cout<<"The string after trim is /""<<x.rtrim()<<"/"."<<endl;
xstring x2(" leading_space");
x2.rtrim();
cout<<"The string after rtrim is /""<<x2<<"/"."<<endl;
xstring x3("trailing_space ");
cout<<"The string after rtrim is /""<<x3.rtrim()<<"/"."<<endl;
xstring x4("trailing_space ");
x4.rtrim();
cout<<"The string after rtrim is /""<<x4<<"/"."<<endl;
xstring x_null("");
cout<<"The /"/" after rtrim is /""<<x_null.rtrim()<<"/"."<<endl;
xstring x_space(" ");
cout<<"The /" /" after rtrim is /""<<x_space.rtrim()<<"/"."<<endl;
xstring x_space2(" ");
cout<<"The /" /" after rtrim is /""<<x_space2.rtrim()<<"/"."<<endl;
xstring x_table(" begin and end are table ");
cout<<"The xstring after rtrim is /""<<x_table.rtrim()<<"/"."<<endl;
xstring x_enter("/nbegin and end are //n./n");
cout<<"The xstring after rtrim is /""<<x_enter.rtrim()<<"/"."<<endl;
xstring mass(" /n mass massing /n /r ");
cout<<"The xstring after rtrim is /""<<mass.rtrim()<<"/"."<<endl;
}
std::cout<<xstring::parse(50)<<std::endl;
xstring x_lower("asd asdf a634sd fjslk&^$%#//|uihkjjk uiy jg ");
cout<<"Lower: "<<x_lower.tolower()<<endl;
cout<<"Upper: "<<x_lower.toupper()<<endl;
xstring x_mash("aWd F asf a634sd fjslk&^$%#|uiGehguihkjjk uiy jg ");
x_mash.toupper();
cout<<"Upper: "<<x_mash<<endl;
x_mash.tolower();
cout<<"Lower: "<<x_mash<<endl;
string str("abc");
xstring x23;
x23=str;
cout<<"x is "<<x23<<endl;
cout<<"Test str2int()"<<endl;
try {
cout<<"123: "<<xstring::str2int("123")<<endl;
cout<<"1: "<<xstring::str2int("1")<<endl;
cout<<"0: "<<xstring::str2int("0")<<endl;
cout<<"1234567890: "<<xstring::str2int("1234567890")<<endl;
cout<<"-1234567890: "<<xstring::str2int("-1234567890")<<endl;
cout<<"000999: "<<xstring::str2int("000999")<<endl;
cout<<"1A90: "<<xstring::str2int("1A90")<<endl;
} catch (string s) {
cout<<"Catch exception, it's "<<s<<endl;
}
cout<<endl<<"Test isnumber"<<endl;
xstring num1("1234");
cout<<num1<<": "<<num1.isnumber()<<endl;
xstring num2("-3587");
cout<<num2<<": "<<num2.isnumber()<<endl;
xstring num3("3a587");
cout<<num3<<": "<<num3.isnumber()<<endl;
cout<<endl<<"Test tolong"<<endl;
xstring num11("1234");
cout<<num11<<": "<<num11.tolong()<<endl;
xstring num22("-123456781");
cout<<num22<<": "<<num22.tolong()<<endl;
/*
xstring x24;
x24="1234567890";
cout<<"StrToInt():"<<x24<<"->"<<x24.StrToInt(10)<<endl;
x24="ABCDEF";
cout<<"StrToInt():"<<x24<<"->"<<x24.StrToInt(16)<<endl;
*/
cout<<"Test end."<<endl;
return 0;
}