xstring(继承了stl的string)

xstring是一个基于STL string扩展的库,提供了丰富的字符串处理功能,如trim、转换为大小写及数字等。本文介绍了xstring的主要功能、用法及示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 注:如果还有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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值