65.9K
CodeProject 正在变化。 阅读更多。
Home

CEnBSTR - _bstr_t 的简单扩展

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.88/5 (5投票s)

2001年2月25日

viewsIcon

80082

downloadIcon

830

一个简单的 _bstr_t 扩展,用于封装基本字符串功能(查找、mid、替换等)

引言

Microsoft Visual C++ 提供的 _bstr_t 类是一个基本的字符串类,它封装了 BSTR API 的复杂细节,此外还提供了引用计数、一些有用的转换运算符和字符串比较功能。

但是,它不支持字符串操作、搜索和子字符串提取。 CEnBSTR 旨在提供您期望在字符串类中找到的一些最常见的功能。 由于 CEnBSTR 继承自 _bstr_t,因此它应该很容易在需要时插入。

分析

  • int find(const _bsrt_t& bstr) const
  • int find(const wint_t& ch) const
  • int find(const _bstr_t& bstr, const unsigned int nStart) const
  • int find(const wint_t& ch, const unsigned int nStart) const
这些函数搜索字符串中的子字符串或单个字符,并返回找到的第一个字符的位置。 如果搜索不成功,将返回 -1。 nStart 参数将设置从零开始的起始位置。

提取

CEnBSTR mid(const unsigned int nFirst, unsigned int nCount = 0) const
此函数将从nFirst字符开始提取 nCount 个字符到一个新的 CEnBSTR 中。 如果 nCount 为 0,则将提取从 nFirst 开始的整个字符串。 nFirst 参数是从零开始的索引。
CEnBSTR left(const unsigned int nCount)
此函数将字符串开头的 nCount 个字符提取到一个新的 CEnBSTR 中。 如果 nCount 超过字符串的长度,将返回整个字符串。
CEnBSTR right(const unsigned int nCount)
此函数将字符串末尾的 nCount 个字符提取到一个新的 CEnBSTR 中。 如果 nCount 超过字符串的长度,将返回整个字符串。
CEnBSTR spanInclude(const _bstr_t& bstrCharset) const
此函数提取第一个子字符串,其中包含在 bstrCharset 中的字符。 结果作为新的 CEnBSTR 返回。
CEnBSTR spanExclude(const _bstr_t& bstrCharset) const
此函数提取第一个子字符串,其中包含不在 bstrCharset 中的字符。 结果作为新的 CEnBSTR 返回。

操作

void upper
此函数将字符串转换为大写。
void lower
此函数将字符串转换为小写。
void reverse
此函数将反转字符串。
unsigned int replace(const _bstr_t& bstrFind, const _bstr_t& bstrReplace)
此函数将所有出现的 bstrFind 替换为 bstrReplace。 将返回出现的次数。
unsigned int replace(const wint_t& chFind, const wint_t& chReplace)
此函数将所有出现的 chFind 替换为 chReplace。 将返回出现的次数。
© . All rights reserved.