Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

XString - non-MFC non-STL string functions

4.91/5 (16 votes)
7 Aug 2007CPOL2 min read 1   916  
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)