Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CEnBSTR - A simple _bstr_t extension

0.00/5 (No votes)
29 Sep 2001 1  
A simple _bstr_t extension to wrap basic string functionality (find, mid, replace aso)

Introduction

The _bstr_t class provided with Microsoft Visual C++ is basic string class that wraps the dirty details of the BSTR API in addition to providing reference counting, some useful cast operators and string comparison features.

However there is no support for string manipulation, searching and substring extraction. CEnBSTR is designed to provide some of the most common features you would expect to find in a string class. Since CEnBSTR inherits _bstr_t, it should be relatively easy to drop in where required.

Analysis

  • 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
These functions search the string for a substring or a single character and returns the position of the first character found. -1 will be returned if the search is unsuccessful. The nStart parameter will set the zero-based start position.

Extraction

CEnBSTR mid(const unsigned int nFirst, unsigned int nCount = 0) const
This function extracts the nCount characters starting at the nFirst character into a new CEnBSTR. If nCount is 0 the entire string starting with nFirst will be extracted. The nFirst parameter is a zero-based index.
CEnBSTR left(const unsigned int nCount)
This function extracts the nCount characters at the beginning of the string into a new CEnBSTR. If the nCount exceeds the length of the string, the entire string will be returned.
CEnBSTR right(const unsigned int nCount)
This function extracts the nCount characters at the end of the string into a new CEnBSTR. If the nCountexceeds the length of the string, the entire string will be returned.
CEnBSTR spanInclude(const _bstr_t& bstrCharset) const
This function extracts the first substring with characters in bstrCharset. The result as returned as a new CEnBSTR.
CEnBSTR spanExclude(const _bstr_t& bstrCharset) const
This function extracts the first substring with characters not in bstrCharset. The result as returned as a new CEnBSTR.

Manipulation

void upper
This function converts the string to uppercase.
void lower
This function converts the string to lowercase.
void reverse
This function will reverse the string.
unsigned int replace(const _bstr_t& bstrFind, const _bstr_t& bstrReplace)
This function replaces every occurrence of bstrFind with bstrReplace. The number of occurrences will be returned.
unsigned int replace(const wint_t& chFind, const wint_t& chReplace)
This function replaces every occurrence of chFind with chReplace. The number of occurrences will be returned.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here