#Creo 二次开发-字符串

以下内容为本人在程序开发过程中遇到的问题及相应解决方法,可能有不正确或不准确的地方,欢迎指正。

wchar_t 输出

宽字符类型要本地化,否则输不出想要结果。
本地化有三条语句可以使用,任取其一。最后一句是全局函数,前两个是 wcout 的一个成员函数的两种表达方式。
std::wcout.imbue(std::locale(“chs”));
std::wcout.imbue(std::locale(""));
setlocale(LC_ALL,“Chinese-simplified”);

xrstring 定义

头文件<ciptype.h>中对 crstring 类型进行了定义

typedef const char         	*xrstring;
# define xstringuninit  	((xrstring) 2)
# define xstringnil     	((const char *) 1)
# define xwstringnil    	((const wchar_t *) 1)
# define xwstringuninit     ((const wchar_t *) 2)

xstring 类

  1. 初始化

头文件<xstring.h>xstring 类进行了定义,其构造函数可以用常用字符串类型进行初始化。

xstring ();
    xstring (const xstring &str);
# ifdef PTC_CXX11_RVALUE_REFERENCES
    xstring (xstring &&str);
    xstring	&operator= (xstring &&str);
# endif
    xstring (const xstring_c &str, bool attach = true);
    xstring (cStringT str);
    xstring (cWStringT str);
    xstring (cStringT str1, cStringT str2);
    inline xstring (const xstring &str1, cStringT str2);
    xstring (cWStringT str1, cWStringT str2);
    inline xstring (const xstring &str1, cWStringT str2);
    xstring (cStringT str, int len);
    xstring (cWStringT str, int len);
    xstring (cStringT str, int from, int to);
    xstring (cWStringT str, int from, int to);
    xstring (strstream &stream);
    xstring (stringstream &stream);
    xstring (wstringstream &stream);
  • 合并字符串:xstring (cStringT str1, cStringT str2)和 xstring (cWStringT str1, cWStringT str2)可以将两个字符串合并
    string s1 = "这是第一个测试!";
    string s2 = "This is first test!";
    xstring xs1(s1.c_str(), s2.c_str());
    cout << "xs1:" << gconvert::UTF8ToGBK(xs1) << endl;
    
    xs1 的输出结果为:
    xs1:这是第一个测试!This is first test!
  • 截取字符串:以下构造函数可以截取字符串的一部分来初始化 xstring 类,形成新的字符串。
    xstring (cStringT str, int len);
    xstring (cWStringT str, int len);
    xstring (cStringT str, int from, int to);
    xstring (cWStringT str, int from, int to);
    
    英文字符串直接对应字符数量,中文字符串的长度没太弄明白。
    根据 UTF-8 编码,一个中文字符占 3 个字节,从计算的字符串长度看,xstring 对象中文字符长度为 UTF-8 编码的一半,也就是两个中文字符占位 3 个字节。实际测试过程中也发现,只能截取偶数个中文字符,截取奇数时会带一个?。
    string s1 = "这是第一个测试!";
    xstring xs1(s1.c_str());
    cout << "s1len:" << s1.length() << endl;
    cout << "xs1len:" << xs1.GetLength() << endl;
    
    输出结果为:
    s1len:24
    xs1len:12
  1. 字符串转换
    以下成员函数可以将字符串转为整形和浮点型,还可以全部转为大写或小写
    bool			ToInt (int *out_val) const;
    bool			ToDouble (double *out_val) const;

    xstring			ToUpper () const;
    xstring			ToLower () const;
  1. 拆分字符串
    GetNWords()可以获取分割后单词个数,默认分隔符为" \t\n"。GetWord()获取相应位置单词。
    int				GetNWords (cStringT seps = " \t\n") const;
    int				GetNWords (cWStringT seps) const;
    inline int 		GetNWords (const xstring &seps) const;

    xstring			GetWord (int idx, cStringT seps = " \t\n") const;
    xstring			GetWord (int idx, cWStringT seps) const;
    inline xstring	GetWord (int idx, const xstring &seps) const;
    string s2 = "This is first test!";
    xstring xs2(s2.c_str());
    for (int i = 0; i < xs2.GetNWords(); i++)
    {
        cout << "Word" << i << ":" << xs2.GetWord(i) << endl;
    }

输出结果为:

Word0:This
Word1:is
Word2:first
Word3:test!
  1. 截取字符串
    string s2 = "This is first test!";
    xstring xs2(s2.c_str());
    cout << xs2.Substring(9, 12) << endl;

输出结果为:
irs 5. 替换字符串

    string s2 = "This is first test!";
    xstring xs2(s2.c_str());
    xs2.Substitute("i", "a");
    cout << xs2 << endl;

输出结果为:
Thas as farst test!
6. 其它成员函数

  • 下面两个函数可以将 xstring 类转为 multibyte 和 wide 字符串,空字符串分别返回 xstringnil 和 xwstringnil。
    inline operator cStringT () const;
    inline operator cWStringT () const;
  • Assign()可以用一个 xstring_c 对象给 xstring 对象赋值,注意这里参数类型为 xstring_c
    xstring &Assign (const xstring_c &str, bool attach = true);
  • 判断是否为空和 NULL
    inline bool		IsNull () const;
    inline bool		IsEmpty () const;

xstring_c

xstring.getxstring_c ()返回该类型变量。

typedef struct __xstring_c
{
	void		*buffer;
	StringT		mbstr;
	WStringT		wcstr;

} xstring_c;
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值