Examples
String manipulation methods
CdString SubStr(int nStartPosition, int nCount)
Returns the nCount number of characters from the start position.
E.g.:
CdString data = "Muehlbauer";
data.SubStr(0,4);
Output:
Mueh
int Find(const CdString & sSearchString, int nOccurrence = 1)
Returns the postion of the given string.
E.g.:
CdString data = "Muehlbauer";
data.Find("e");
Output:
2
int Compare(const char * szOther )
Returns the bool value after comparing two strings.
E.g.:
CdString sData1 = "Muehlbauer";
CdString sData2 = "Muehlbauer";
sData1.Compare(sData2);
Output:
-1
| Return value | Indicates |
|---|---|
| <0 | The first character that does not match or has a lower value in sData1 than in sData2 |
| 0 | The contents of both strings are equal |
| >0 | The first character that does not match or has a greater value in sData1 than in sData2 |
CdString & Append( const char cOther )
Returns the length of the string.
E.g.:
CdString sData1 = "Muehlbauer";
CdString sData2 = " je super";
sData1.Append(sData2);
Output:
"Muehlbauer je super"
bool Empty()
Clears the variable data.
E.g.:
CdString data = "Muehlbauer";
data.Empty();
bool MakeLower()
Changes the string to lower case.
E.g.:
CdString data = "Muehlbauer";
data.MakeLower();
muehlbauer
bool MakeUpper()
Changes the string to upper case.
E.g.:
CdString data = "Muehlbauer";
data.MakeLower();
MUEHLBAUER
Get methods
int GetLength()
Returns the length of the string.
E.g.:
CdString data = "Muehlbauer";
data.GetLength();
Output:
10
bool IsEmpty()
Checks whether the given string is empty.
E.g.:
CdString data = "Muehlbauer";
data.IsEmpty();
Output:
false
Code
CdString sStr1 = "Muehlbauer";
CdString sStr2 = "ja super";
//SubStr
po_ELOG("CdStringMethods") << sStr1.SubStr(0,4) << CLOSELOG; //Outputs: Mueh
//Find
po_ELOG("CdStringMethods") << sStr1.Find('e') << CLOSELOG; //Outputs: 2
//Compare
po_ELOG("CdStringMethods") << sStr1.Compare(sStr2) << CLOSELOG; //Outputs: -1
//GetLength
po_ELOG("CdStringMethods") << sStr1.GetLength() << CLOSELOG; //Outputs: 10
//IsEmpty
po_ELOG("CdStringMethods") << sStr1.IsEmpty() << CLOSELOG; //Outputs: (bool)false
//MakeLower
sStr1.MakeLower();
po_ELOG("CdStringMethods") << sStr1 << CLOSELOG; //Outputs: muehlbauer
//MakeUpper
sStr1.MakeUpper();
po_ELOG("CdStringMethods") << sStr1 << CLOSELOG; //Outputs: MUEHLBAUER
//Append
sStr1.Append(sStr2);
po_ELOG("CdStringMethods") << sStr1 << CLOSELOG; //Outputs: MUEHLBAUERja super
//Empty
sStr1.Empty();
po_ELOG("CdStringMethods") << sStr1 << CLOSELOG;//Empty string
Operators
Code:
CdString sStr1 = "Hello";
CdString sStr2 = "World";
// Concatenation
CdString sStr3 = sStr1 + " " + sStr2;
po_ELOG("CdStringMethods") << "Concatenated String: " << sStr3 << CLOSELOG;
// Using concatenation assignment operator
sStr1 += "!";
po_ELOG("CdStringMethods") << "After appending: " << sStr1 << CLOSELOG;
// Equality checks
po_ELOG("CdStringMethods") << "Is sStr1 equal to sStr3? " << (sStr1 == sStr3) << CLOSELOG;
po_ELOG("CdStringMethods") << "Is sStr1 not equal to sStr3? " << (sStr1 != sStr3) << CLOSELOG;
// Accessing characters using indexing operator
po_ELOG("CdStringMethods") << "First character of sStr1: " << sStr1[0] << CLOSELOG;
sStr1[0] = 'h';
po_ELOG("CdStringMethods") << "Modified sStr1: " << sStr1 << CLOSELOG;
sStr1 = "Apple";
sStr2 = "Banana";
// Comparison operators
po_ELOG("CdStringMethods") << "Is sStr1 < sStr2? " << (sStr1 < sStr2) << CLOSELOG;
po_ELOG("CdStringMethods") << "Is sStr1 > sStr2? " << (sStr1 > sStr2) << CLOSELOG;
po_ELOG("CdStringMethods") << "Is sStr1 <= sStr2? " << (sStr1 <= sStr2) << CLOSELOG;
po_ELOG("CdStringMethods") << "Is sStr1 >= sStr2? " << (sStr1 >= sStr2) << CLOSELOG;
// Conversion operator
const char* cstr = sStr1; // Implicit conversion to const char*
po_ELOG("CdStringMethods") << "C-style string: " << cstr << CLOSELOG;//Outputs: Apple
Concatenated String: Hello World
After appending: Hello!
Is sStr1 equal to sStr3? (bool)false
Is sStr1 not equal to sStr3? (bool)true
First character of sStr1: 'H'
Modified sStr1: hello!
Is sStr1 < sStr2? (bool)true
Is sStr1 > sStr2? (bool)false
Is sStr1 <= sStr2? (bool)true
Is sStr1 >= sStr2? (bool)false
C-style string: Apple