Click here to Skip to main content
15,886,872 members
Articles / Programming Languages / C++
Article

CEnBSTR - A simple _bstr_t extension

Rate me:
Please Sign up or sign in to vote.
4.88/5 (5 votes)
29 Sep 2001 79.4K   829   29   6
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


Written By
Norway Norway
Morten Abrahamsen is a software architect living in Oslo, Norway. He is currently designing and developing a commerce platform using Microsoft .Net technologies. He has extensive experience with the .Net Framework, COM+, SQL Server 2000, Visual C++/ATL and Visual C#.

Comments and Discussions

 
QuestionLicense agreement? Pin
Francisco José Sen del Prado24-Jun-09 3:40
Francisco José Sen del Prado24-Jun-09 3:40 
GeneralReplace functions for the CEnBSTR Class Pin
Alpesh Makwana26-Feb-01 22:52
Alpesh Makwana26-Feb-01 22:52 
GeneralRe: Replace functions for the CEnBSTR Class Pin
Morten Abrahamsen27-Feb-01 8:46
Morten Abrahamsen27-Feb-01 8:46 
GeneralRe: Replace functions for the CEnBSTR Class Pin
Morten Abrahamsen28-Feb-01 8:56
Morten Abrahamsen28-Feb-01 8:56 
I've added my versions of the replace functions.

I hope you like them Wink | ;)
GeneralConversion from UCHR* to BSTR and vice-versa Pin
Anand Tati7-Oct-02 7:13
Anand Tati7-Oct-02 7:13 
GeneralRe: Conversion from UCHR* to BSTR and vice-versa Pin
Anonymous8-Oct-02 0:58
Anonymous8-Oct-02 0:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.