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
    XString test("Test");
Like Find, Remove or Replace, but case-insensitiv (No Case) test.Find("E")
    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.


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"
Return the given element as specified type str.GetString(3)
SetElement Set the element at the given index with a
string, double, int, char or bool
str.SetElement(3, 99)
str.SetElement(9, 'x')
ElementIndex Retrieve the index of the specified element str.ElementIndex("four")
Sort the elementstring by using the specified type str.ElementSortString()
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, '#')


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)
Repeat Create a XString with copies of a given string or char Repeat('x', 10)
Repeat("Test", 3")
GetStringTableEntry Return the entry of a stringtableresource GetStringTableEntry(
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