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