Strings

Chapter Updated 06/19/25




XBase64 includes a class (xbString) for handling strings. It is portable and designed to work the same across all platforms.

Class xbString methods

MethodDescriptionParms
xbString(xbUInt32 size)
xbString(char cIn)
xbString(const char *sIn, xbUInt32 ulMaxLen)
xbString(const xbString &sIn)
xbString(const char *sIn = "")
xbString( xbDouble dIn )
Various constructors. cIn - Input character
sIn - Pointer to char string
ulMaxLen - Max length of input string to use
dIn - Double value to convert to a string
xbString &operator= (const xbString &sIn)
xbString &operator= (const char *sIn)
Assignment operator. sIn - Input pointer to char string
operator const char *() const* operator returns pointer to string data.
char &operator[](xbUInt32 n) const
char &operator[](xbInt32 n) const
[] operator. Returns a character at position n. First position in string is 1 (not 0). n - Offset ino the string for the byte to retrieve
xbString &operator+=(const xbString &s)
xbString &operator+=(const char *s)
xbString &operator+=(char c)
Append operator.
xbString &operator-=(const xbString &s)
xbString &operator-=(const char *s)
xbString &operator-=(char c)
Append operator, right trimming both strings.
xbBool operator == ( const xbString& ) const
xbBool operator == ( const char * ) const
Equals comparison operator.
xbBool operator != ( const xbString& ) const
xbBool operator != ( const char * ) const
Not equals comparison operator.
xbBool operator < ( const xbString& ) constLess than operator.
xbBool operator > ( const xbString& ) constGreater than operator.
xbBool operator <= ( const xbString& ) const/td>Less than or equals operator.
xbBool operator >= ( const xbString& ) const/td>Greater than or equals operator.
xbString operator-( const xbString &s ) Concatonate operator. Concatonate left string with right string returning reference to new string. Both strings are trimmed.
xbString operator+( const char *s )
xbString operator+( const xbString &s )
xbString operator+( const char c )
Concatonate operator. Concatonates left string with right string returning reference to new string.
xbString &AddBackSlash( char c )Add a prefixing back slash to specified characters in the string. c - Charater to precede with a backslash.
xbString &Append(const xbString &s)
xbString &Append(const char *s)
xbString &Append(const char *s, xbUInt32 ulByteCount )
xbString &Append(char c)
Append data to the string. s - Data to append to string.
ulByteCnt - Max number of bytes to append to string.
c - Char value to append to string.
xbString &Assign(const char *srcStr, xbUInt32 ulStartPos, xbUInt32 ulCopyLen )
xbString &Assign(const char *srcStr, xbUInt32 ulStartPos )
xbString &Assign(const xbString &srcStr, xbUInt32 ulStartPos, xbUInt32 ulCopyLen )
xbString &Assign(const xbString &srcStr, xbUInt32 ulStartPos )
Assign portion of a string. srcStr - Source string.
ulStartPos - Starting position. The first position is 1.
ulCopyLen - Max copy length.
xbString Copy() constCopy a string
xbUInt32 CountChar( char c ) const
xbUInt32 CountChar( char c, xbInt16 iOpt ) const
Count the number of characters in a string. c - Character to count.
iOpt - Option
  • 0 - Count the number of characters.
  • 1 - Count the number of characters not between single or double quotes.
  • xbInt16 CvtHexChar( char &cOut ) Convert a four byte string in the format of 0x00 to a one byte char value. The first four bytes of the string must be in the format 0x00. Anything past the first four bytes is disregarded. cOut - Output character.
    xbInt16 CvtHexString( xbString &sOut ) This routine converts a string of one or more four byte sequences 0x00 to a string of one byte chars. sOut - Output string.
    xbInt16 CvtULongLong( xbUInt64 &ullOut )Convert string to xbUInt64 number. ullOut - Output unsigned 64 bit interger.
    xbInt16 CvtLongLong( xbInt64 &llOut )Convert string to xbInt64 number llOut - Output signed 64 bit interger.
    void Dump( const char *sTitle ) const
    void Dump( const char *sTitle, xbInt16 iOption ) const
    void DumpHex( const char *sTitle ) const
    void Dump( const xbString &sTitle ) const
    void Dump( const xbString &sTitle, xbInt16 iOption ) const
    void DumpHex( const xbString &sTitle ) const
    Dump string contents to stdout.
    Only available if compile option XB_DEBUG_SUPPORT is on.
    iOption Output option.Mbr>
  • 0 - Print character values.
  • 1 - Print as hex data.
  • xbString &ExtractElement(xbString &sIn, char delim, xbUInt32 iCnt, xbInt16 iOpt = 0 )
    xbString &ExtractElement(const char *sIn, char delim, xbUInt32 iCnt, xbInt16 iOpt = 0 )
    Extract an element from a delimited string. sIn - Input string to extract element from
    delim - One byte delimiter
    iCnt - Number of delimiters to skip
    iOpt - Quote handling option
  • 0 - ignore single and double quotes.
  • 1 - ignore delimiters between single or double quotes.
  • char GetCharacter( xbUInt32 ulPos ) const Extract character from position ulPos. First position is 1. ulPos - Position within string of character to extract.
    xbUInt32 GetLastPos(char c) const
    xbUInt32 GetLastPos(const char *s) const
    Get last position of element within string. c - character to retrieve the last position in string for.
    s - string to retrieve the last position in string for.
    char GetPathSeparator() const Returns the path separator.
    Returns \ for Windows environment.
    Returns / for *nix environment.
    xbUInt32 GetSize() const
  • Returns number of bytes allocated to the data portion of the string including the null termination byte.
  • Returns zero if null.
  • Not to be confused with the Len() method which returns the lenght of the actual string data.
  • xbBool HasAlphaChars() const Determine if the string has any alpha characters.
  • Returns xbTrue if string contains one or more alpha characters.
  • Returns xbFalse if string contains no alpha characters.
  • xbBool IsEmpty() const Determine if string is empty.
  • Returns xbTrue if string is empty.
  • Returns xbFalse if string is not empty.
  • xbBool IsNull() const Determine if string is null.
  • Returns xbTrue if string is null.
  • Returns xbFalse if string is not null.
  • xbString &Left( xbUInt32 ulLen )
    Retain left part of string, Truncare rightmost excess. ulLen - New string length.
    xbUInt32 Len() const
  • Returns the length of the string excluding the terminating null byte.
  • Not to be confused with GetSize() method which returns the memory allocation size.
  • xbString &Ltrim() Left trim whitespace from string.
    xbString &Ltrunc( xbUInt32 ulCnt ) Left truncate string. ulCnt - Number of bytes to remove from the left.
    xbString &Mid(xbUInt32 ulPos, xbUInt32 lLen ) Extract portion of data from string. ulStartPos - Starting position.
    ulTargLen - Length.
    xbString &PadLeft( char c, xbUInt32 ulLen ) Left pad string. c - Padding character.
    ulNewLen - New string length.
    xbString &PadRight( char c, xbUInt32 ulLen ) Right pad string. c - Padding character.
    ulNewLen - New string length.
    xbUInt32 Pos(char c, xbUInt32 ulStartPos ) const
    xbUInt32 Pos(char c) const
    xbUInt32 Pos(const char *s) const
    Determine position of a given character or string. c - Seek character.
    s - Seek string.
    ulStartPos - Starting position for search, first position is 1.
    xbString &PutAt(xbUInt32 ulPos, char c) Insert character into string. ulPos - Insertion position.
    c - Character to insert.
    xbString &Remove( xbUInt32 ulPos, xbUInt32 ulN ) Remove portion of string. ulStartPos - Starting position for removal operation.
    ulDelSize - Size of deletion.
    xbString &Replace( const char *sReplace, const char *sReplaceWith, xbInt16 iOpt = 0 )
    xbString &Replace( const xbString &sReplace, const xbString &sReplaceWith, xbInt16 iOpt = 0 )
    xbString &Replace( constxbString &sReplace, const char *sReplaceWith, xbInt16 iOpt = 0 )
    xbString &Replace( const char *sReplace, const xbString &sReplaceWith, xbInt16 iOpt = 0 )
    Replace a value within a string with another value. sReplace - Character string to replace.
    sReplaceWith - Character string to replace with.
    iOption - Processing option.
  • 0 = All occurrences
  • 1 = first occurrence
  • xbString &Resize( xbUInt32 lSize ) Resize a string. ulSize - New string size, including null termination byte.
    xbString &Rtrim() Right trim whitespace from string.
    xbString &Set( const char *s )
    xbString &Set( const xbString &s )
    xbString &Set( const char *s, xbUInt32 ulSize )
    Set the value of the string. s - Value to set the string.
    ulSize - Maximum size of resultant string.
    xbString &SetNum( xbInt32 lNum ) Set the string to long integer numeric value. lNum - Value to set the string.
    xbString &Sprintf(const char *format, ...)
  • Routine for formatting a string.
  • This routine mimics the standard C printf() routine except
  • it creates a string instead of writing to stdout.
  • See documentation on the standard C printf function for how to set up the formats.

    Example: MyString.Sprintf( "Num: %d Text %s", 100, "test data" )
  • sFormat - A format specifier.
    ... - variables or data values to insert into formatted string.
    const char *Str() const Return string data. Returns char * to string data or NULL if string is empty
    char *strncpy( char * cDest, xbUInt32 n ) const
  • Copy all or part of string to character array.
  • It is the responsibility of the application to verify the buffer is large enough to hold the string contents.
  • cDest - pointer to destination buffer.
    n Number of bytes to copy.
    xbString &SwapChars( char from, char to ) Swap characters. cFrom - Character to replace.
    cTo - Character to replace with.
    xbString &ToLowerCase() Replace all upper case charaters with lower case characters.
    xbString &ToUpperCase() Replace all lower case charaters with upper case characters.
    xbString &Trim() Right and left trim whitespace from string.
    xbBool ValidLogicalValue() const
  • Returns xbTrue if string is valid logical data else returns xbFalse.
  • Valid logical data is one 'T', 'F', 'N' or 'Y'.
  • xbBool ValidNumericValue() const This function returns true if the string contains valid numeric data, else returns false.
    xbString &ZapChar( char c ) Remove every instance of specified character from a string. c - Character to remove from string.
    xbString &ZapLeadingChar( char c ) Remove leading character from a string. c - Character to remove from beginning of string.
    xbString &ZapTrailingChar( char c ) Remove trailing character from a string. c - Character to remove from ending of string.
    std::ostream& operator<< ( std::ostream& os, const xbString& s ) Stream insertion operator << os - Output stream.
    s - String to send to output stream





    Example program using xbString class

    /* xb_ex_string.cpp XBase64 Software Library Copyright (c) 1997,2003,2014,2021,2022 Gary A Kunkel The xb64 software library is covered under the terms of the GPL Version 3, 2007 license. Email Contact: XDB-devel@lists.sourceforge.net XDB-users@lists.sourceforge.net */ // This demonstrates the string class #include "xbase.h" using namespace xb; int main() { // create a string, assign a value, print it xbString s1; s1 = "Test String 1"; fprintf( stdout, "s1 = [%s]\n", s1.Str()); // create another string, copy the value from s1 into it, print it xbString s2; s2 = s1; std::cout << "s2 = [" << s2.Str() << "]" << std::endl; // create another string with and print it xbString s3( 'X' ); std::cout << "s3 = [" << s3.Str() << "]" << std::endl; // create another string with and print it and print it out yet another way xbString s4( "Class constructor test 4" ); printf( "s4 = [%s]\n", s4.Str() ); // create another string with a size limit and print it out xbString s5( "Class constructor test 4", 7 ); printf( "s5 = [%s]\n", s5.Str() ); // create another string from a string xbString s6( s5 ); printf( "s6 = [%s]\n", s6.Str() ); // create 100 byte string with nothing in it xbString s7( (xbUInt32) 100 ); printf( "s7 = [%s]\n", s7.Str() ); // Extract character from a particular position in the string printf( "[] test -- Position 7 (starts from 1) from String 1 = [%c]\n", s1[7] ); // or use the getCharacter method printf( "getCharacter() test -- Position 7 (starts from 1) from String 1 = [%c]\n", s1.GetCharacter(7) ); // set string 7 to a character s7 = 'Z'; printf( "updated s7 = [%s]\n", s7.Str() ); // trim methods s3 = " abc "; s3.Ltrim(); #ifdef XB_DEBUG_SUPPORT s3.Dump( "LTrim test" ); #else std::cout << s3.Str() << std::endl; #endif s3 = " abc "; s3.Rtrim(); std::cout << "RTrim test - " << s3.Str() << std::endl; s3.Trim(); std::cout << "Trim test - " << s3.Str() << std::endl; printf( "s3 Len = [%d]\n", s3.Len() ); // Concatenation tests - I s1 = "Concatenation test1 part 1 "; s1 += "Concatenation test1 part 2 "; s2 = " s2 data "; s1 += s2; s1 += 'z'; // Concatenation tests - II s1 = "Concatenation test1 part 1 "; s1 -= "Concatenation test1 part 2 "; s1 -= 'X'; s1 -= s2; std::cout << "Concatenation test 2 - " << s1.Str() << std::endl; // Concatenation tests - III s1 = "s1data "; s2 = "s2data "; s3 = s1 - s2; std::cout << "Concatenation test 3a - " << s3.Str() << std::endl; s3 = s1 + s2; std::cout << "Concatenation test 3b - " << s3.Str() << std::endl; s3 = s1 + " char * data "; std::cout << "Concatenation test 3c - " << s3.Str() << std::endl; s3 = s1 + 'Z'; std::cout << "Concatenation test 3d - " << s3.Str() << std::endl; s3 = 'A'; std::cout << s3.Str() << std::endl; s3 += s1; std::cout << s3.Str() << std::endl; // The following compiles and runs, but is not valid // s3 = 'A' + s1; std::cout << std::endl << "== operator tests" << std::endl; if( s1 == s2 ) std::cout << s1.Str() << " == " << s2.Str() << std::endl; else std::cout << s1.Str() << " != " << s2.Str() << std::endl; s1 = s2; if( s1 == s2 ) std::cout << s1.Str() << " == " << s2.Str() << std::endl; else std::cout << s1.Str() << " != " << s2.Str() << std::endl; if( s1 == "sometestdata" ) std::cout << s1.Str() << " == sometestdata" << s2.Str() << std::endl; else std::cout << s1.Str() << " != sometestdata" << s2.Str() << std::endl; std::cout << std::endl << "!= operator tests" << std::endl; s2 = "abc123"; std::cout << "s1 - " << s1.Str() << std::endl; std::cout << "s2 - " << s2.Str() << std::endl; if( s1 != s2 ) std::cout << s1.Str() << " != " << s2.Str() << std::endl; else std::cout << s1.Str() << " == " << s2.Str() << std::endl; s1 = s2; if( s1 != s2 ) std::cout << s1.Str() << " != " << s2.Str() << std::endl; else std::cout << s1.Str() << " == " << s2.Str() << std::endl; if( s1 != "sometestdata" ) std::cout << s1.Str() << " != [sometestdata]" << std::endl; else std::cout << s1.Str() << " == [sometestdata]" << std::endl; std::cout << std::endl << "< operator tests" << std::endl; s1 = "AAA"; s2 = "BBB"; if( s1 < s2 ) std::cout << s1.Str() << " < " << s2.Str() << std::endl; else std::cout << s1.Str() << " >= " << s2.Str() << std::endl; s1 = "BBB"; if( s1 < s2 ) std::cout << s1.Str() << " < " << s2.Str() << std::endl; else std::cout << s1.Str() << " >= " << s2.Str() << std::endl; s1 = "CCC"; if( s1 < s2 ) std::cout << s1.Str() << " < " << s2.Str() << std::endl; else std::cout << s1.Str() << " >= " << s2.Str() << std::endl; std::cout << std::endl << "> operator tests" << std::endl; s1 = "AAA"; s2 = "BBB"; if( s1 > s2 ) std::cout << s1.Str() << " > " << s2.Str() << std::endl; else std::cout << s1.Str() << " <= " << s2.Str() << std::endl; s1 = "BBB"; if( s1 > s2 ) std::cout << s1.Str() << " > " << s2.Str() << std::endl; else std::cout << s1.Str() << " <= " << s2.Str() << std::endl; s1 = "CCC"; if( s1 > s2 ) std::cout << s1.Str() << " > " << s2.Str() << std::endl; else std::cout << s1.Str() << " <= " << s2.Str() << std::endl; std::cout << std::endl << "<= operator tests" << std::endl; s1 = "AAA"; s2 = "BBB"; if( s1 <= s2 ) std::cout << s1.Str() << " <= " << s2.Str() << std::endl; else std::cout << s1.Str() << " > " << s2.Str() << std::endl; s1 = "BBB"; if( s1 <= s2 ) std::cout << s1.Str() << " <= " << s2.Str() << std::endl; else std::cout << s1.Str() << " > " << s2.Str() << std::endl; s1 = "CCC"; if( s1 <= s2 ) std::cout << s1.Str() << " <= " << s2.Str() << std::endl; else std::cout << s1.Str() << " > " << s2.Str() << std::endl; std::cout << std::endl << ">= operator tests" << std::endl; s1 = "AAA"; s2 = "BBB"; if( s1 >= s2 ) std::cout << s1.Str() << " >= " << s2.Str() << std::endl; else std::cout << s1.Str() << " < " << s2.Str() << std::endl; s1 = "BBB"; if( s1 >= s2 ) std::cout << s1.Str() << " >= " << s2.Str() << std::endl; else std::cout << s1.Str() << " < " << s2.Str() << std::endl; s1 = "CCC"; if( s1 >= s2 ) std::cout << s1.Str() << " >= " << s2.Str() << std::endl; else std::cout << s1.Str() << " < " << s2.Str() << std::endl; std::cout << "(const char *) " << (const char *) s2.Str() << std::endl; std::cout << std::endl << "CountChar() test" << std::endl; s1 = "ABADFDSGA"; xbUInt32 i = s1.CountChar( 'A' ); std::cout << "There are " << i << " 'A's in " << s1.Str() << std::endl; s1.Ltrunc( 4 ); std::cout << "lTunc(4) test s1 = [" << s1.Str() << "]" << std::endl; std::cout << std::endl << "PutAt() test" << std::endl; s1.PutAt( 3, 'Z' ); std::cout << "Third char should be a 'Z' = " << s1.Str() << std::endl; std::cout << std::endl << "AddBackSlash() test" << std::endl; s1.AddBackSlash( 'Z' ); std::cout << "Should be a backslash before the 'Z' = " << s1.Str() << std::endl; std::cout << std::endl << "Assign() test" << std::endl; s2 = "1234567890"; std::cout << "s2 = " << s2.Str() << std::endl; s1.Assign( s2, 4, 5 ); std::cout << "assign( s2, 4, 5 ) results = " << s1.Str() << std::endl; s1.Assign( s2, 4, 15 ); std::cout << "assign( s2, 4, 15 ) results = " << s1.Str() << std::endl; s1.Assign( s2, 5 ); std::cout << "Assign( s2, 5 ) results = " << s1.Str() << std::endl; s1.Assign( s2, 15 ); std::cout << "Assign( s2, 15 ) results = " << s1.Str() << std::endl; std::cout << std::endl << "s1.copy() test" << std::endl; s1 = "ABC"; std::cout << "s1 = " << s1.Str() << std::endl; std::cout << "s2 = " << s2.Str() << std::endl; s1 = s2.Copy(); std::cout << "s1.Copy() results" << s1.Str() << std::endl; s1 = "0x35"; char hexChar; s1.CvtHexChar( hexChar ); std::cout << "CvtHexChar test [" << s1.Str() << "] converts to [" << hexChar << "]" << std::endl; s1 = "0x65"; s1.CvtHexChar( hexChar ); std::cout << "cvHexChar test [" << s1.Str() << "] converts to [" << hexChar << "]" << std::endl; s1 = "0x610x620x630x640x65"; s1.CvtHexString( s2 ); std::cout << "CvtHexString [" << s1.Str() << "] converts to [" << s2.Str() << "]" << std::endl; s1.ExtractElement( "aaaa|bbbb|cccc|dddd", '|', 2, 0 ); std::cout << "ExtractElement() " << s1.Str() << std::endl; s1 = "123"; s2 = "ABC"; std::cout << "HasAlphaChars( " << s1.Str() << " ) = " << s1.HasAlphaChars() << std::endl; std::cout << "HasAlphaChars( " << s2.Str() << " ) = " << s2.HasAlphaChars() << std::endl; s2 = ""; std::cout << "IsEmpty( " << s1.Str() << " ) = " << s1.IsEmpty() << std::endl; std::cout << "IsEmpty( " << s2.Str() << " ) = " << s2.IsEmpty() << std::endl; s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; std::cout << s1.Str() << " s1.Mid( 3,5 ) = [" << s1.Mid( 3, 5 ).Str() << "]" << std::endl; std::cout << s1.Str() << " s1.Mid( 25, 10 ) = [" << s1.Mid( 25, 10 ).Str() << "]" << std::endl; std::cout << s1.Str() << " s1.Pos('G') = " << s1.Pos( 'G' ) << std::endl; std::cout << s1.Str() << " s1.Pos(\"JKL\") = " << s1.Pos( "JKL" ) << std::endl; std::cout << "Remove( 3, 5 ) before " << s1.Str() << std::endl; std::cout << s1.Str() << " s1.Remove( 3, 5 ) = [" << s1.Remove( 3, 5 ).Str() << "]" << std::endl; std::cout << "Remove( 3, 5 ) after " << s1.Str() << std::endl; s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; std::cout << "s1.Remove( 20, 10 ) = [" << s1.Remove( 20, 10 ).Str() << "]" << std::endl; std::cout << "Remove( 20, 10 ) - " << s1.Str() << std::endl; s1.Sprintf( "%d", 12345 ); std::cout << "Sprintf( %d, 12345 ) " << s1.Str() << std::endl; s1.SetNum( (long) 123456 ); std::cout << "s1.SetNum( 123456 ) = " << s1.Str() << std::endl; s1.Set( "Yet another way to set a string value" ); std::cout << "Set - " << s1.Str() << std::endl; s1 = "ABCABCABZ"; std::cout << "SwapChars( 'A', '9' ) before - " << s1.Str() << std::endl; s1.SwapChars( 'A', '9' ); std::cout << "SwapChars( 'A', '9' ) after - " << s1.Str() << std::endl; s1.ToLowerCase(); std::cout << "ToLowerCase - " << s1.Str() << std::endl; s1.ToUpperCase(); std::cout << "ToUpperCase - " << s1.Str() << std::endl; s1.ZapChar( '9' ); std::cout << "ZapChar( '9' )" << s1.Str() << std::endl; s1.ZapLeadingChar( 'B' ); std::cout << "ZapLeadingChar( 'B' )" << s1.Str() << std::endl; s1.ZapTrailingChar( 'Z' ); std::cout << "ZapLeadingChar( 'Z' ) - " << s1.Str() << std::endl; s1 = "123"; s1.PadLeft( '0', 9 ); std::cout << "s1.PadLeft('0', 9 ) - " << s1.Str() << std::endl; s1 = "abc"; s1.PadRight( 'Z', 9 ); std::cout << "s1.PadRight('Z', 9 ) " << s1.Str() << std::endl; xbString sNullString; if( sNullString.IsNull()) std::cout << "sNullString is null" << std::endl; else std::cout << "sNullString is not null" << std::endl; xbString tstS( "ZZZZZZZZZ" ); tstS = s1.Left( 5 ); std::cout << "tstS = " << tstS.Str() << "\n"; std::cout << "s1 = " << s1.Str() << "\n"; tstS = "1234567890"; std::cout << "mid result = " << tstS.Mid( 3, 3 ).Str() << std::endl; tstS = "1234567890"; std::cout << "left result = " << tstS.Left( 3 ).Str() << std::endl; return 0; }