Extended Strings

The XString-class is a powerfull extension to the standard CString-class. Whereever you use a CString you can now use a XString with much more functionality. The class includes many string-functions I missed in CString, like removing, replacing or inserting strings into another, convertingfunctions to double or int and case-insensitive find, replace and remove. But the most powerfull extension is a method I called 'Elementstrings'. Most of the functions are const and return a XString, so you can use them in an expression. The functionality of this class is not very difficult to understand or to implement, but in my case, it saves a lot of time in my projects.

If you miss some functions which could be usefull to extend this class please contact me.

Additional String functions
What are Elementstrings?
Elementstring functions
Friend functions

Download source file (8 KB) (comments in german)

Additional Stringfunctions:

Function Description Example Result
    XString num("12.345");
Double Convertion to double num.Double() 12.345
Int Convertion to int num.Int() 12
Char Convertion to char num.Char() 1
Bool Convertion to bool num.Bool() true
    XString test("Test Test");
Remove Remove first occurence of a string, char or range test.Remove('e') "Tst Test"
RemoveAll Remove all occurences of a string or char test.RemoveAll("e") "Tst Tst"
Replace Replace first occurence of a string, char or range with another string test.Replace("x", "e") "Txst Test"
ReplaceAll Replace all occurences of a string or char with another string test.ReplaceAll("x", "e") "Txst Txst"
Insert Inserts a string at a given position test.Insert("e", 1) "Teest Test"
    XString op("Test");
XString leer;
operator-- Remove last character of the string op--, --op "Tes"
operator! Indicates, wheter the string was empty !op
!lee
false
true
    XString test("Test");
NCFind
NCRemove
NCReplace
Like Find, Remove or Replace, but case-insensitiv (No Case) test.Find("E")
test.NCFind("E")
-1
1
    XString test(" Test ");
Reverse Reverses the string test.Reverse() " tseT "
Trim Delete leading and trailing whitespaces test..Trim() "Test"
Lower Returns lowercase string test.Lower() " test "
Upper Returns uppercase string test.Upper() " TEST "
    XString test("Test Test");
FromTo Like Mid, but using positions test.FromTo(5, 7) "Tes"
Count Counts the occurence of a string or char in another string test.Count('e') 2
    XString test("Test")
FindIndex Search the position of the first occurence of a char test.FindIndex('s') 2
Fill Fill the string to the specified length with trailing spaces test.Fill(5) "Test "
    XString error("Error * in Line *");
ReplaceTabs Replace all \t in the string with the corresponding number of spaces    
ReplaceText Replace '*' with specified text.
When using more than one '*' devide the replace-texts with '|'
error.ReplaceText("75|120") "Error 75 in Line 120"
    XString format("###.##");
GetDoubleFormatString Translate a formatting-string into C-notation format.GetDoubleFormatString() "%6.2lf"

What are Elementstrings?

Elementstrings are a special sort of short (and dynamic) stringarrays. Every array-entry was divided with a separator from the next entry. For example, the elementstring "zero|one|two|three" has four elemententrys. With this class you can easyly get, set or find a specified element. You don't have to specify the size of the 'array', because it's only one string. The entries can be strings, chars, doubles, ints or bools. If you set an entry, the numbers are converted into strings and if you get one, it was back converted to the specified type. Normaly the separator is '|', but it's possible to use every other character.

Elementstringfunctions:

Function Description Example Result
    XString str("zero|one|22|3.3|four|5||7")
Element Return the given element as string str.Element(1) "one"
Elements Return the given elements as an elementstring str.Elements(1, 4) "one|22|3.3|four"
operator() same as Element or Elements str(3) "3.3"
GetString
GetDouble
GetInt
GetChar
GetBool
Return the given element as specified type str.GetString(3)
str.GetDouble(3)
str.GetInt(3)
str.GetChar(3)
str.GetBool(3)
"3.3"
3.3
3
'3'
true
SetElement Set the element at the given index with a
string, double, int, char or bool
str.SetElement(3, 99)
str.SetElement(9, 'x')
"zero|one|22|99|four|5||7"
"zero|one|22|99|four|5||7||x"
ElementIndex Retrieve the index of the specified element str.ElementIndex("four")
str.ElementIndex(1)
4
-1
ElementSortString
ElementSortDouble
ElementSortInt
ElementSortChar
ElementSort
Sort the elementstring by using the specified type str.ElementSortString()
str.ElementSortInt()
str.ElementSortDouble()
"|22|3.3|5|7|four|one|zero"
"0|0|0|0|3|5|7|22"
"0|0|0|0|3.3|5|7|22"
       
SetSeparator Defines a new separator XString::SetSeparator('#')  
RestoreSeparator Restores temporary separators XString::RestoreSeparator()  
    XString str("one#two#three")
You can use all functions with an additional parameter as temporar separator. str.Element(1)
str.Element(1, '#')
"one#two#three"
"two"

Friendfunctions:

Function Description Example Result
itoa Create a XString from an int itoa(5) "5"
utoa Create a XString from an unsinged int utoa(2) "2"
dtoa Create a XString from a doube dtoa(3.14)
dtoa(3.14, 5)
"3.14"
"3.14000"
Repeat Create a XString with copies of a given string or char Repeat('x', 10)
Repeat("Test", 3")
"xxxxxxxxxx"
"TestTestTest"
GetStringTableEntry Return the entry of a stringtableresource GetStringTableEntry(
AFX_IDS_IDLEMESSAGE)
"Ready"
Concat Create an elementstring of 1 to 16 strings Concat("This", "Is", "A", "Test") "This|Is|A|Test"

 

If you've questions, contact me at Joachim.Raidl@iname.com

Last updated: 8 May 1998



Comments

  • C# conversion of RemoveAll method

    Posted by davepusey on 06/04/2005 09:38am

    I have converted the RemoveAll method to C#. Here is the source code...
    
    private static string RemoveAll(string Input, char CharToRemove)
    {
      string TempString = Input;
    
      while (TempString.IndexOf(CharToRemove) >= 0)
      {
        TempString = TempString.Remove(TempString.IndexOf(CharToRemove),1);
      }
    
      return TempString;
    }

    Reply
  • help me~~~~

    Posted by Legacy on 05/12/2003 12:00am

    Originally posted by: Joli

     I'm beginners in programming, and studing about string.
    
    I made project file with MFC VC++ and this errors come out.

    Linking...
    nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol
    __endthreadex
    nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol
    __beginthreadex
    Debug/XString.exe : fatal error LNK1120: 2 unresolved externals
    Error executing link.exe.
    Creating browse info file...
    XString.exe - 3 error(s), 0 warning(s)

    What should i do? Please Help me.

    Reply
  • Simple modification for use in VC++ .NET

    Posted by Legacy on 08/12/2002 12:00am

    Originally posted by: Josh

    To use in VC++ .NET simply replace m_pchData with *this and replace array accesses with the GetAt function.

    • Please explain changes for VC7 .NET

      Posted by solion on 10/07/2004 10:32am

      >replace array accesses with the GetAt function. I compiled without changing arrays, only replaced m_pchData with *this. What did you mean by replace with GetAt ? Can you give an example, please ?

      Reply
    Reply
  • Problems in VC++ .NET 7.0

    Posted by Legacy on 02/13/2002 12:00am

    Originally posted by: Joe

    do not use this if your in VC++ 7.0.

    Reply
  • String Extension

    Posted by Legacy on 06/07/2001 12:00am

    Originally posted by: Devi

    How to convert date in String format to Date?

    For eg , a sample date string is

    "January , 05 June,2000"

    Reply
  • Se puede usar en lugar de ' char * ' y ' const char* '

    Posted by Legacy on 09/28/2000 12:00am

    Originally posted by: William Gonz�les

    Hola,
    �Se puede usar esta clase en par�metros de funciones de tipo ' char * ' y ' const char * ' ?

    La clase CString tiene el operador ' const char* ', pero hay muchas funciones que tienen par�metros de tipo
    ' char * ' usadas para recibir en ellas una cadena de caracteres, la clase CString se puede usar aqu� con las funciones GetBuffer() y ReleaseBuffer(), pero me obliga a limitar mi cadena de caracteres a un n�mero fijo y si la cadena que voy a recibir es mas larga, empiezan los problemas.

    Gracias,

    William Gonz�les S.

    Reply
  • Comment-out "#include NoDump.h" ( Answer from Joachim)

    Posted by Legacy on 02/05/1999 12:00am

    Originally posted by: Jong-Yun Moon

    Comment-out "#include NoDump.h".

    When I asked to Joachim, he told me that it was just for debugging, so should comment-out.

    Reply
  • I Don't Know....NoDump.h....Please Help Me!!!

    Posted by Legacy on 01/22/1999 12:00am

    Originally posted by: Lee Jung Ryong

    It's important work..
    I must know rapidly What "NoDump.h" is working for...
    And, Please Let me Know where to get it...
    Please Help me....

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds