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

XString - non-MFC non-STL string functions

Rate me:
Please Sign up or sign in to vote.
4.91/5 (16 votes)
7 Aug 2007CPOL2 min read 60.8K   912   33   10
XString offers functions for remove, replace, trim, and case-insensitive search that are not included in the CRT.

Introduction

XString is a collection of non-MFC and non-STL string functions I have been using for many years. They are all modelled on functions available in standard C runtime (CRT), so they are fairly self-explanatory.

While there might be better or more efficient alternatives to some of these functions available via STL or boost, in the real world I am frequently constrained to use what my clients want. In some cases, they do not want the integration, support, and QA problems associated with introducing new technology into legacy applications. Hence, being able to show them string functions that "look like" the CRT, but without any STL, is often deciding point, and greatly simplifies my work.

XString Demo

The included demo app performs some basic tests, and also shows how XString functions are used:

screenshot

Function Descriptions

  • _tcscrep() - Replace character in a string (case sensitive)
    ///////////////////////////////////////////////////////////////////////////////
    //
    // _tcscrep()
    //
    // Purpose:     Replace character in a string (case sensitive)
    //
    // Parameters:  str     - pointer to string; upon return, str will be updated 
    //                        with the character replacements
    //              chOld   - character to look for
    //              chNew   - character to replace it with
    //
    // Returns:     TCHAR * - Pointer to the updated string. Because the 
    //                        modification is done in place, the pointer  
    //                        returned is the same as the pointer passed 
    //                        as the input argument. 
    //
  • _tcsicrep() - Replace character in a string (case insensitive)
    ///////////////////////////////////////////////////////////////////////////////
    //
    // _tcsicrep()
    //
    // Purpose:     Replace character in a string (case insensitive)
    //
    // Parameters:  str   - pointer to string; upon return, str will be updated 
    //                      with the character replacements
    //              chOld - character to look for
    //              chNew - character to replace it with
    //
    // Returns:     TCHAR * - Pointer to the updated string. Because the 
    //                        modification is done in place, the pointer  
    //                        returned is the same as the pointer passed 
    //                        as the input argument. 
    //
  • _tcsicrem() - Remove character in a string (case insensitive)
    ///////////////////////////////////////////////////////////////////////////////
    //
    // _tcsicrem()
    //
    // Purpose:     Remove character in a string (case insensitive)
    //
    // Parameters:  str - pointer to string; upon return, str will be updated 
    //                    with the character removals
    //              ch  - character to remove
    //
    // Returns:     TCHAR * - Pointer to the updated string. Because the 
    //                        modification is done in place, the pointer  
    //                        returned is the same as the pointer passed 
    //                        as the input argument. 
    //
  • _tcscrem() - Remove character in a string (case sensitive)
    ///////////////////////////////////////////////////////////////////////////////
    //
    // _tcscrem()
    //
    // Purpose:     Remove character in a string (case sensitive)
    //
    // Parameters:  str - pointer to string; upon return, str will be updated 
    //                    with the character removals
    //              ch  - character to remove
    //
    // Returns:     TCHAR * - Pointer to the updated string. Because the 
    //                        modification is done in place, the pointer  
    //                        returned is the same as the pointer passed 
    //                        as the input argument. 
    //
  • _tcsistrrem() - Remove substring in a string (case insensitive)
    ///////////////////////////////////////////////////////////////////////////////
    //
    // _tcsistrrem()
    //
    // Purpose:     Remove substring in a string (case insensitive)
    //
    // Parameters:  str    - pointer to string; upon return, str will be updated 
    //                       with the substring removals
    //              substr - substring to remove
    //
    // Returns:     TCHAR * - Pointer to the updated string. Because the 
    //                        modification is done in place, the pointer  
    //                        returned is the same as the pointer passed 
    //                        as the input argument. 
    //
  • _tcsstrrem() - Remove substring in a string (case sensitive)
    ///////////////////////////////////////////////////////////////////////////////
    //
    // _tcsstrrem()
    //
    // Purpose:     Remove substring in a string (case sensitive)
    //
    // Parameters:  str    - pointer to string; upon return, str will be updated 
    //                       with the substring removals
    //              substr - substring to remove
    //
    // Returns:     TCHAR * - Pointer to the updated string. Because the 
    //                        modification is done in place, the pointer  
    //                        returned is the same as the pointer passed 
    //                        as the input argument. 
    //
  • _tcsistr() - Find a substring in a string (case insensitive)
    ///////////////////////////////////////////////////////////////////////////////
    //
    // _tcsistr()
    //
    // Purpose:     Find a substring in a string (case insensitive)
    //
    // Parameters:  str     - pointer to string; upon return, str will be updated 
    //                        with the character removals
    //              substr  - substring to find
    //
    // Returns:     TCHAR * - Pointer to the first occurrence of substr in str, 
    //                        or NULL if substr does not appear in string.  If 
    //                        substr points to a string of zero length, the 
    //                        function returns str.
    //
  • _tcsistrrep() - Replace one substring in a string with another substring (case insensitive)
    ///////////////////////////////////////////////////////////////////////////////
    //
    // _tcsistrrep()
    //
    // Purpose:     Replace one substring in a string with another 
    //              substring (case insensitive)
    //
    // Parameters:  lpszStr    - Pointer to string; upon return, lpszStr will be 
    //                           updated with the character removals
    //              lpszOld    - Pointer to substring that is to be replaced
    //              lpszNew    - Pointer to substring that will replace lpszOld
    //              lpszResult - Pointer to buffer that receives result string.  
    //                           This may be NULL, in which case the required size
    //                           of the result buffer will be returned. (Call
    //                           _tcsistrrep once to get size, then allocate 
    //                           buffer, and call _tcsistrrep again.)
    //
    // Returns:     int        - Size of result string.  If lpszResult is NULL,
    //                           the size of the buffer (in TCHARs) required to 
    //                           hold the result string is returned.  Does not 
    //                           include terminating nul character.  Returns 0
    //                           if no replacements made.
    //
  • _tcstrim() - Removes (trims) leading and trailing whitespace characters from a string
    ///////////////////////////////////////////////////////////////////////////////
    //
    // _tcstrim()
    //
    // Purpose:     Removes (trims) leading and trailing whitespace characters 
    //              from a string
    //
    // Parameters:  str     - Pointer to the null-terminated string to be trimmed. 
    //                        On return, str will hold the trimmed string.
    //              targets - Pointer to string containing whitespace characters.
    //                        If this is NULL, a default set of whitespace
    //                        characters is used.
    //
    // Returns:     TCHAR * - Pointer to trimmed string
    //
  • _tcsrtrim() - Removes (trims) trailing whitespace characters from a string
    ////////////////////////////////////////////////////////////////////////
    //
    // _tcsrtrim()
    //
    // Purpose:     Removes (trims) trailing whitespace characters from a string
    //
    // Parameters:  str     - Pointer to the null-terminated string to be trimmed. 
    //                        On return, str will hold the trimmed string.
    //              targets - Pointer to string containing whitespace characters.
    //                        If this is NULL, a default set of whitespace
    //                        characters is used.
    //
    // Returns:     TCHAR * - Pointer to trimmed string
    //
  • _tcsltrim() - Removes (trims) leading whitespace characters from a string
    ///////////////////////////////////////////////////////////////////////////////
    //
    // _tcsltrim()
    //
    // Purpose:     Removes (trims) leading whitespace characters from a string
    //
    // Parameters:  str     - Pointer to the null-terminated string to be trimmed. 
    //                        On return, str will hold the trimmed string.
    //              targets - Pointer to string containing whitespace characters.
    //                        If this is NULL, a default set of whitespace
    //                        characters is used.
    //
    // Returns:     TCHAR * - Pointer to trimmed string
    //
  • _tcszdup() - Allocates buffer with new, fills it with zeros, copies string to buffer
    ///////////////////////////////////////////////////////////////////////////////
    //
    // _tcszdup()
    //
    // Purpose:     Allocates buffer with new, fills it with zeros, copies string
    //              to buffer
    //
    // Parameters:  str     - Pointer to the null-terminated string to be copied. 
    //
    // Returns:     TCHAR * - Pointer to duplicated string (allocated with new)
    //
  • _tcsnzdup() - Allocates buffer with new, fills it with zeros, copies count characters from string to buffer
    ///////////////////////////////////////////////////////////////////////////////
    //
    // _tcsnzdup()
    //
    // Purpose:     Allocates buffer with new, fills it with zeros, copies count
    //              characters from string to buffer
    //
    // Parameters:  str     - Pointer to the null-terminated string to be copied
    //              count   - Number of characters to copy
    //
    // Returns:     TCHAR * - Pointer to duplicated string (allocated with new)
    //
  • _tcsccnt() - Count number of occurrences of a character in a string
    ///////////////////////////////////////////////////////////////////////////////
    //
    // _tcsccnt()
    //
    // Purpose:     Count number of occurrences of a character in a string
    //
    // Parameters:  str - pointer to string
    //              ch  - character to look for
    //
    // Returns:     int - Number of times ch is found in str
    //

How To Use

To integrate XString functions into your app, you first need to add following files to your project:

  • XString.cpp
  • XString.h

If you include XString in project that uses precompiled headers, you must change C/C++ Precompiled Headers settings to Not using precompiled headers for XString.cpp.

Next, include the header file XString.h in appropriate project files. Now you are ready to start using XString. Please see XStringTest.cpp for examples.

Revision History

Version 1.1 — 2007 August 7

  • Added _tcszdup()
  • Added _tcszndup()
  • Added _tcsccnt()

Version 1.0 — 2007 June 25

  • Initial public release

Usage

This software is released into the public domain. You are free to use it in any way you like, except that you may not sell this source code. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions

 
GeneralMy vote of 1 Pin
xComaWhitex22-Jan-10 18:38
xComaWhitex22-Jan-10 18:38 
GeneralRe: My vote of 1 Pin
tian1022224-Nov-11 20:40
tian1022224-Nov-11 20:40 
GeneralSuper share Pin
jasonp1229-Dec-09 7:39
jasonp1229-Dec-09 7:39 
AnswerRe: Super share Pin
Hans Dietrich26-Apr-11 18:39
mentorHans Dietrich26-Apr-11 18:39 
GeneralSubstring replacement Pin
Metaspace20-Aug-07 22:05
Metaspace20-Aug-07 22:05 
Nice library! Have been using the very efficient white space trimming functions of you for a while already, no I discovered this. Thank you for sharing.

Two things concerning substring replacement:

(1) This function either returns size of result string, or size plus result string. In the former case, the function is inefficient, as result length could be determined by counting occurrences of the substring to be replaced, then adding to the original string size the product of this number with the size difference of old and new substring.
Instead, you create the result string in any case, discarding it if the caller does not need it.
If the caller DOES need it, you copy from the buffer allocated by the function to that provided by the user. Would be better to create result there in the first place.

(2) To further increase efficiency, it would not be necessary to allocate any buffer at all if the result string's size is equal or smaller than the original one (of course you then cannot use strncpy).
AnswerRe: Substring replacement Pin
Hans Dietrich26-Apr-11 18:38
mentorHans Dietrich26-Apr-11 18:38 
GeneralDifferences to CString of MFC Pin
KarstenK13-Aug-07 21:13
mveKarstenK13-Aug-07 21:13 
GeneralRe: Differences to CString of MFC Pin
Hans Dietrich13-Aug-07 22:20
mentorHans Dietrich13-Aug-07 22:20 
GeneralZip file format Pin
visualc25-Jun-07 23:14
visualc25-Jun-07 23:14 
GeneralRe: Zip file format Pin
Hans Dietrich26-Jun-07 1:54
mentorHans Dietrich26-Jun-07 1:54 

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.