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

XString - non-MFC non-STL string functions

, 7 Aug 2007
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Hans Dietrich
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 PinmemberxComaWhitex22-Jan-10 18:38 
GeneralRe: My vote of 1 Pinmembertian1022224-Nov-11 20:40 
GeneralSuper share Pinmemberjasonp1229-Dec-09 7:39 
AnswerRe: Super share PinmentorHans Dietrich26-Apr-11 18:39 
GeneralSubstring replacement PinmemberMetaspace20-Aug-07 22:05 
AnswerRe: Substring replacement PinmentorHans Dietrich26-Apr-11 18:38 
GeneralDifferences to CString of MFC PinmemberKarstenK13-Aug-07 21:13 
GeneralRe: Differences to CString of MFC PinmvpHans Dietrich13-Aug-07 22:20 
GeneralZip file format Pinmembervisualc25-Jun-07 23:14 
Can you provide the zip archive in old zip format ? The current zip file is in Zip 2.0 format and requires Winzip 11.

GeneralRe: Zip file format PinmvpHans Dietrich26-Jun-07 1:54 

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 | Mobile
Web01 | 2.8.140814.1 | Last Updated 7 Aug 2007
Article Copyright 2007 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid