Click here to Skip to main content
Click here to Skip to main content

CEnBSTR - A simple _bstr_t extension

, 29 Sep 2001
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

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? Pinmemberfjosesen24-Jun-09 3:40 
GeneralReplace functions for the CEnBSTR Class PinmemberAlpesh Makwana26-Feb-01 22:52 
GeneralRe: Replace functions for the CEnBSTR Class PinmemberMorten Abrahamsen27-Feb-01 8:46 
GeneralRe: Replace functions for the CEnBSTR Class PinmemberMorten Abrahamsen28-Feb-01 8:56 
GeneralConversion from UCHR* to BSTR and vice-versa PinsussAnand Tati7-Oct-02 7:13 
GeneralRe: Conversion from UCHR* to BSTR and vice-versa PinsussAnonymous8-Oct-02 0:58 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141030.1 | Last Updated 30 Sep 2001
Article Copyright 2001 by Morten Abrahamsen
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid