[ <- Previous Page ] [ ^ Table Of Contents ^ ] [ Next Page -> ]
zString Class

Chapter 5.1
zString Class

The zString class is used for manipulating string data. String data is defined as one or more contiguous bytes of data with a Null (0x00) terminating end byte.

Newer versions of C++ include a string class. When this software was initially developed, there was not a standard portable C++ string class available, so this class was created and has been carried forward.


zString API

Method/FunctionDescriptionReturn Codes
zString ( ) This is the default constructor for the zString class.
zString (size_t size) This constructor initializes this zString to length of size.
zString (char c) Constructor initializes this zString with char c data.
zString (const char * s) Constructor initializes this zString with s.
zString (const char *s, size_t maxlen) Constructor initializes this zString with s and maximum length of maxlen.
zString (const zString &s) Constructor initializes this zString with data referenced by zString s.
~zString ( ) This is the class destructor.
zString &operator = (const zString &s) This operator is used to set the zString value to s.
zString &operator = (const char *s) This operator is used to set the zString value to s.
zString &operator = (char c); This operator is used to set the zString value c.
zString &operator += (const char *s) This operator appends data s to this zString.
zString &operator += (char c) This operator appends data c to this zString.
char operator[ ] (int pos) This operator returns the character at position pos.
char getChar (int pos) This method returns the character at position pos.
operator const char *( ) const; This operator returns a pointer to the data of the zString.
void resize (size_t size) This method updates the size of the string to new length of size. Size includes the null terminating byte.
void setNum (long num) This method sets this zString to the numeric value num.
void putAt (size_t pos, char c) This method puts character c at position pos of this zString.
void toLowerCase ( ); This method converts all upper case data in this zString to lower case.
void toUpperCase ( ); This method converts all lower case data in this zString to upper case.
void trim ( ) Trim all spaces from the end of this zString.
void swapChars ( char s, char t) Swap all characters s with character t.
void init (const char * data) Initializes string to data.
void zapChar (char c) Delete every occurrence of character c.
void addBackSlash ( char c) Prefixes all occurrences of character c with a backslash.
void lTrunc ( size_t ) Left truncate len bytes.
Void zapLeadingChar ( char c ) Remove leading character c from the beginning of the string.
bool isNull ( ) const Returns true if the string has no memory allocated for the string, false otherwise.
bool isEmpty ( ) const Returns true if the string has no memory allocated, or memory allocated is a zero byte string, otherwise it returns false.
bool hasAlphaChars ( ) const Returns true if the string has any alpha characters.
int pos ( char c ) const Return the position in the zString of the first occurence of character c.
Int strpos (const char * s) const Return the position of substring s in this zString.
int countChar (char c) const Return the count of character c in this zString.
size_t len ( ) const; Returns the length of the zString. It does not include the null terminating byte.
zString copy ( ) const; This method is used for copying a zString.
zString &sprintf (const char *iformat, ...); This method formats the string to the supplied format, iformat. See the standard C printf function for formatting details.
zString& assign (const zString& str, size_t pos =0, int len = -1) Assign data in str, starting at position pos for a length of len and return a reference.
const char *getData ( ) const; This method returns a pointer to the string data.
bool operator == (const zString& s1, const char *s2) Compares one zString to another. Returns true if zStrings match.
bool operator != ( const zString& s1, const char *s2) Compares one zString to another. Returns true if zStrings do not match.
bool operator == ( const zString& s1, char *s2) Compares zString to char string s2. Returns true if equal.
bool operator != ( const zString& s1, char *s2) Compares zString to char string. Returns true if not equal.
zString operator + ( const zString& s1, const zString& s2) Appends zString s2 to zString s1.
zString operator + ( const zString& s1, const char *s2) Appends char string s2 to zString s1.
zString operator + ( const char *s1, const zString& s2) Appends zString s2 to char string s1.
zString operator + ( const zString& s1, char c2) Appends char c2 to zString s1.
zString operator + ( const c1, const zString& s2) Appends zString s2 to char c1.
zString operator + ( const zString& s1, const unsigned long 1) Appends long 1 to zString s1.
int cvt HexString (zString &out) Converts a string in format 0x00 to a one byte char and places it in out -1=Error
0=Success
int cvtHexChar (char &out) Converts a single four byte string in the format of 0x00 to a one byte char value. First four bytes must be 0x00; anything past four bytes is disregarded. -1=Error
0=Success
void frameIt (const &zString &bfc, const zString &efc) Frame string beginning with bfc and ending with efc.
int deframeIt (const *zString &bfc, const zString &efc) Remove framing chars bfc from string. -1=Chars do not match, action failed





zString Program Example

/* zstring1 test program

   Copyright (c) 2000 HSC  GNU/GPL 
 
   This test program demonstrates example uses of the zString class.
   
*/

#include  
#include "../../base/zstring.cpp"

using namespace std;

int main()
{
  int i;
  zString str1;
  zString str2;
  zString str3;

  cout << "String Test Program" << endl;

  str1 = "string test data";
  cout << "operator = test ->" << str1 << endl;

  str1.resize( 10 );
  cout << "resize test ->" << str1 << endl;
  
  i = str1.len();
  cout << "new length = " << i << endl;
  
  str1 = "StRiNg TeSt DaTa";
  str1.toLowerCase();
  cout << "lower case test = " << str1 << endl;
  str1.toUpperCase();
  cout << "upper case test = " << str1 << endl;

  str1 = "part 1 ";
  str2 = "part 2";
  str3 = str1 + str2;

  cout << "str1 = " << str1 << endl;
  cout << "str2 = " << str2 << endl;
  cout << "str3 = " << str3 << endl; 
  
  str3 += " part3 ";
  cout << "str3 again = " << str3 << endl;
  cout << "str3[3] = " << str3[3] << endl;

  str1.setNum( 10 );
  cout << "str1.setNum(10) = " << str1 << endl;

  str1.sprintf( "%s %d %s", "some text", 5, "more text" );
  cout << "sprintf test = " << str1 << endl;
  
  return 0;
}



[ <- Previous Page ] [ ^ Table Of Contents ^ ] [ ^ Top Of Page ^ ] [ Next Page -> ]


Visit the GNU home page.

FSF & GNU inquiries & questions to gnu@gnu.org.

Comments on these web pages to info@zhsac.com.

Copyright (C) 2003 HealthCare Systems and Consulting

Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved.

Last updated: 07/21/2003