Files Class List
String Class Reference

Detailed Description

A general-purpose string class for char arrays.

This is a general purpose string class, which supports many useful string operations. It has methods for numeric conversions, character and string search, formating, editing, and file operations.

#include <cyString.h>

Public Member Functions

Constructors and Destructor
 String ()
 
 String (const String &src)
 
 String (const char *src)
 
 String (char src)
 
 String (int src)
 
 String (long src)
 
 String (float src)
 
 String (double src)
 
 String (const char *src, unsigned int count)
 
 String (double src, int digits, int precisition)
 
 String (int size, const char *format,...)
 
virtual ~String ()
 
Set methods (Assign value)
StringSet (const String &str)
 
StringSet (const char *src)
 
StringSet (char src)
 
StringSet (int src)
 
StringSet (long src)
 
StringSet (float src)
 
StringSet (double src)
 
StringSet (const char *src, unsigned int count)
 
StringSet (double src, int digits, int precision)
 
StringSet (int size, const char *format,...)
 
Assignment operators
Stringoperator= (const String &src)
 
Stringoperator= (const char *src)
 
Stringoperator= (char src)
 
Stringoperator= (int src)
 
Stringoperator= (long src)
 
Stringoperator= (float src)
 
Stringoperator= (double src)
 
Editing methods
void EmptyString ()
 
int SetLength (int newLength)
 
void SetCharString (char chr, int count)
 
void Delete (int start, int count)
 
void Insert (const String &str, int index)
 
int DeleteCarriageReturns ()
 
int InsertCarriageReturns ()
 
int DeleteChar (char c)
 
int DeleteChars (const String &set)
 
void Reverse ()
 
void SwapChars (int index1, int index2, int count=1)
 
StringFormat (int size, const char *format,...)
 
char & operator[] (const int index)
 
Get value methods
int Length () const
 
const char * GetString () const
 
int IsEmpty () const
 
char * LastChar () const
 
int SearchCharInSet (int index, const String &set) const
 
int SearchSet (const String &set) const
 
int ReverseSearchSet (const String &set) const
 
int SearchNextSet (int start, const String &set) const
 
int GetPosition (char c) const
 
int GetPosition (const String &str) const
 
int GetLastPosition (char c) const
 
int GetLastPosition (const String &str) const
 
int GetNextPosition (int start, char c) const
 
int GetNextPosition (int start, const String &str) const
 
int GetLineNumber (int pos) const
 
int GetNumLines () const
 
int CountChar (char c) const
 
int CountChar (char c, int start, int end) const
 
int CountChars (const String &set) const
 
char operator[] (const int index) const
 
Comparison methods
int operator== (const String &str) const
 
int operator!= (const String &str) const
 
int operator< (const String &str) const
 
int operator<= (const String &str) const
 
int operator> (const String &str) const
 
int operator>= (const String &str) const
 
int Compare (const String &str) const
 
int Compare (const String &str, int count) const
 
int CompareIC (const String &str) const
 
int CompareIC (const String &str, int count) const
 
int IsCapitalChar (int pos) const
 
int IsLowerCaseChar (int pos) const
 
int IsNumericChar (int pos) const
 
int IsAlphaChar (int pos) const
 
int IsAlphaNumericChar (int pos) const
 
int IsNameChar (int pos) const
 
int IsSpaceChar (int pos) const
 
int IsControlChar (int pos) const
 
Append and substring
void Append (const String &str)
 
Stringoperator+= (const String &str)
 
String operator+ (const String &right)
 
void SubString (int start, int count)
 
String GetSubString (int start, int count) const
 
Conversion to numbers and number arrays
int ToInt () const
 
long ToLong () const
 
float ToFloat () const
 
double ToDouble () const
 
void ToIntArray (int n, int *v) const
 
void ToLongArray (int n, long *v) const
 
void ToFloatArray (int n, float *v) const
 
void ToDoubleArray (int n, double *v) const
 
Trim and shrink
void Trim ()
 
void TrimLeft ()
 
void TrimRight ()
 
void Shrink ()
 
Case change
void LowerCase ()
 
void UpperCase ()
 
void SwapCase ()
 
void TitleCase ()
 
String GetLowerCase () const
 
String GetUpperCase () const
 
String GetSwapCase () const
 
String GetTitleCase () const
 

Friends

std::ostream & operator<< (std::ostream &os, const String &str)
 

Load/save and file name methods

void DecodeFileName (String &path, String &filename)
 
void DecodeFileName (String &path, String &filename, String &extention)
 
int LoadFromStream (FILE *stream, unsigned int count)
 
int LoadFromFile (const String &filename)
 
int SaveToStream (FILE *stream) const
 
int SaveToFile (const String &filename) const
 

Constructor & Destructor Documentation

§ String() [1/11]

String ( )

Default constructor.

§ String() [2/11]

String ( const String src)

Copy constructor.

§ String() [3/11]

String ( const char *  src)

Sets the string using the given array.

§ String() [4/11]

String ( char  src)

Sets the string using the given number.

§ String() [5/11]

String ( int  src)

Default constructor.

§ String() [6/11]

String ( long  src)

Default constructor.

§ String() [7/11]

String ( float  src)

Default constructor.

§ String() [8/11]

String ( double  src)

Default constructor.

§ String() [9/11]

String ( const char *  src,
unsigned int  count 
)

Sets the string using the first 'count' characters of the given array.

§ String() [10/11]

String ( double  src,
int  digits,
int  precisition 
)

Sets the string as the given double number 'src' using given number of digits and precision.

§ String() [11/11]

String ( int  size,
const char *  format,
  ... 
)

Sets the string using the given format and predicted string size. The predicted size should be greater than or equal to the final size.

§ ~String()

virtual ~String ( )
virtual

Destructor.

Member Function Documentation

§ Set() [1/10]

String& Set ( const String str)

Sets the string using the given string. Returns the string.

§ Set() [2/10]

String& Set ( const char *  src)

Sets the string using the given array. Returns the string.

§ Set() [3/10]

String& Set ( char  src)

Sets the string as the given character. Returns the string.

§ Set() [4/10]

String& Set ( int  src)

Sets the string using the given number. Returns the string.

§ Set() [5/10]

String& Set ( long  src)

Sets the string using the given number. Returns the string.

§ Set() [6/10]

String& Set ( float  src)

Sets the string using the given number. Returns the string.

§ Set() [7/10]

String& Set ( double  src)

Sets the string using the given number. Returns the string.

§ Set() [8/10]

String& Set ( const char *  src,
unsigned int  count 
)

Sets the string using the first 'count' characters of the given array. Return this string.

§ Set() [9/10]

String& Set ( double  src,
int  digits,
int  precision 
)

Sets the string as the given double number 'src' using given number of digits and precision. Returns this string.

§ Set() [10/10]

String& Set ( int  size,
const char *  format,
  ... 
)

Sets the string using the given format and predicted string size. The predicted size should be greater than or equal to the final size. Returns this string.

§ EmptyString()

void EmptyString ( )

Converts to empty string.

§ SetLength()

int SetLength ( int  newLength)

Changes the length of the array by keeping the values in the string. If the new size is smaller, a part of the string is lost. If the new size is greater, space characters are added at the end of the string.

§ SetCharString()

void SetCharString ( char  chr,
int  count 
)

Creates a string of length 'count' with the given character 'chr'.

§ Delete()

void Delete ( int  start,
int  count 
)

Deletes 'count' number of characters from the array, starting from 'start'. If 'count' is too big, deletes till the end of the string.

§ Insert()

void Insert ( const String str,
int  index 
)

Inserts the given string to the given index.

§ DeleteCarriageReturns()

int DeleteCarriageReturns ( )

Deletes all carriage return characters. Returns the number of characters deleted.

§ InsertCarriageReturns()

int InsertCarriageReturns ( )

Insert a carriage return character after every new line character if it doesn't already exits. Returns the number of carriage return characters inserted.

§ DeleteChar()

int DeleteChar ( char  c)

Deletes all the characters in the string that are equal to the given character 'c'. Returns the number of characters deleted.

§ DeleteChars()

int DeleteChars ( const String set)

Deletes all the characters in the string that exits in the given set. Returns the number of characters deleted.

§ Reverse()

void Reverse ( )

Reverses the string.

§ SwapChars()

void SwapChars ( int  index1,
int  index2,
int  count = 1 
)

Swap 'count' number of characters starting from 'index1' and 'index2'.

§ Format()

String& Format ( int  size,
const char *  format,
  ... 
)

Sets the string using the given format and predicted string size. The predicted size should be greater than or equal to the final size. Returns this string.

§ operator[]() [1/2]

char& operator[] ( const int  index)

Returns the character of the string at the specified index.

§ Length()

int Length ( ) const

Returns the length of the string.

§ GetString()

const char* GetString ( ) const

Returns the char array that keep the string data.

§ IsEmpty()

int IsEmpty ( ) const

Checks if the string is empty (length is zero).

§ LastChar()

char* LastChar ( ) const

Returns a pointer to the last character before the first null character. If null character is not found, or the string is empty returns nullptr pointer.

§ SearchCharInSet()

int SearchCharInSet ( int  index,
const String set 
) const

If the character at the given index exists in the given set, returns the index of the character. If the character is not found in the set, returns -1.

§ SearchSet()

int SearchSet ( const String set) const

Searches the string starting from the beginning for a character that exists in the given set. Returns the first position of a character from the set. If not found, returns -1.

§ ReverseSearchSet()

int ReverseSearchSet ( const String set) const

Searches the string starting from the end for a character that exists in the given set. Returns the last position of a character from the set. If not found, returns -1.

§ SearchNextSet()

int SearchNextSet ( int  start,
const String set 
) const

Searches the string starting from the beginning for a character that exists in the given set. Returns the first position of a character from the set. If not found, returns -1.

§ GetPosition() [1/2]

int GetPosition ( char  c) const

Returns the first position of the character. If the character is not found, returns -1.

§ GetPosition() [2/2]

int GetPosition ( const String str) const

Returns the first position of the given string. If the string is not found, returns -1.

§ GetLastPosition() [1/2]

int GetLastPosition ( char  c) const

Returns the last position of the character. If the character is not found, returns -1.

§ GetLastPosition() [2/2]

int GetLastPosition ( const String str) const

Returns the last position of the given string. If the string is not found, returns -1.

§ GetNextPosition() [1/2]

int GetNextPosition ( int  start,
char  c 
) const

Returns the next position of the character. If the character is not found, returns -1.

§ GetNextPosition() [2/2]

int GetNextPosition ( int  start,
const String str 
) const

Returns the next position of the given string. If the string is not found, returns -1.

§ GetLineNumber()

int GetLineNumber ( int  pos) const

Returns the line number of the given position in the string.

§ GetNumLines()

int GetNumLines ( ) const

Returns the number of lines in the string.

§ CountChar() [1/2]

int CountChar ( char  c) const

Returns the number of characters in the string that are equal to the given character 'c'.

§ CountChar() [2/2]

int CountChar ( char  c,
int  start,
int  end 
) const

Returns the number of characters in the string that are equal to the given character 'c'.between start and end.

§ CountChars()

int CountChars ( const String set) const

Returns the number of characters in the string that exists in the given character set.

§ operator[]() [2/2]

char operator[] ( const int  index) const

Returns the character of the string at the specified index.

§ operator==()

int operator== ( const String str) const

Checks if this string is the same as 'str'.

§ operator!=()

int operator!= ( const String str) const

Checks if this string is not the same as 'str'.

§ operator<()

int operator< ( const String str) const

Checks if this string is smaller than 'str'.

§ operator<=()

int operator<= ( const String str) const

Checks if this string is smaller than or equal to 'str'.

§ operator>()

int operator> ( const String str) const

Checks if this string is greater than 'str'.

§ operator>=()

int operator>= ( const String str) const

Checks if this string is greater than or equal to 'str'.

§ Compare() [1/2]

int Compare ( const String str) const

Compares this string to 'str' (case sensitive). Returns zero if the strings are the same, smaller than zero if this string is smaller than 'str', greater than zero if this string is greater than 'str'.

§ Compare() [2/2]

int Compare ( const String str,
int  count 
) const

Compares this string to 'str' (case sensitive) limited to the first 'count' characters. Returns zero if the strings are the same, smaller than zero if this string is smaller than 'str', greater than zero if this string is greater than 'str'.

§ CompareIC() [1/2]

int CompareIC ( const String str) const

Compares this string to 'str' (case insensitive). Returns zero if the strings are the same, smaller than zero if this string is smaller than 'str', greater than zero if this string is greater than 'str'.

§ CompareIC() [2/2]

int CompareIC ( const String str,
int  count 
) const

Compares this string to 'str' (case insensitive) limited to the first 'count' characters. Returns zero if the strings are the same, smaller than zero if this string is smaller than 'str', greater than zero if this string is greater than 'str'.

§ IsCapitalChar()

int IsCapitalChar ( int  pos) const

Returns if the char in the given position is a capital letter.

§ IsLowerCaseChar()

int IsLowerCaseChar ( int  pos) const

Returns if the char in the given position is a lower case letter.

§ IsNumericChar()

int IsNumericChar ( int  pos) const

Returns if the char in the given position is a numeric character.

§ IsAlphaChar()

int IsAlphaChar ( int  pos) const

Returns if the char in the given position is a lower case or capital letter.

§ IsAlphaNumericChar()

int IsAlphaNumericChar ( int  pos) const

Returns if the char in the given position is a lower case or capital letter or a numeric character.

§ IsNameChar()

int IsNameChar ( int  pos) const

Returns if the char in the given position is a lower case or capital letter, a numeric character or '_'.

§ IsSpaceChar()

int IsSpaceChar ( int  pos) const

Returns if the char in the given position is a space character.

§ IsControlChar()

int IsControlChar ( int  pos) const

Returns if the char in the given position is a control char (including new line etc.)

§ Append()

void Append ( const String str)

Appends to the end of the string.

§ operator+=()

String& operator+= ( const String str)

Appends to the end of the string and returns this string.

§ operator+()

String operator+ ( const String right)

Appends two strings and returns the result.

§ SubString()

void SubString ( int  start,
int  count 
)

Converts the string to its sub-string starting from 'start' with length of 'count'. If 'count' is too large, returns the sub-string till the end of the string.

§ GetSubString()

String GetSubString ( int  start,
int  count 
) const

Returns the sub-string from 'start' with the length of 'count'. If 'count' is too big, returns the sub-string till the end of the string.

§ ToInt()

int ToInt ( ) const

Converts the string to int.

§ ToLong()

long ToLong ( ) const

Converts the string to long.

§ ToFloat()

float ToFloat ( ) const

Converts the string to float.

§ ToDouble()

double ToDouble ( ) const

Converts the string to double.

§ ToIntArray()

void ToIntArray ( int  n,
int *  v 
) const

Converts the string (ex: "23,43,14") to int array.

§ ToLongArray()

void ToLongArray ( int  n,
long *  v 
) const

Converts the string to long.

§ ToFloatArray()

void ToFloatArray ( int  n,
float *  v 
) const

Converts the string (ex: "23,43,14") to float array.

§ ToDoubleArray()

void ToDoubleArray ( int  n,
double *  v 
) const

Converts the string (ex: "23,43,14") to double array.

§ Trim()

void Trim ( )

Deletes empty characters from the beginning and end of the string.

§ TrimLeft()

void TrimLeft ( )

Deletes empty characters from the beginning of the string.

§ TrimRight()

void TrimRight ( )

Deletes empty characters from the end of the string.

§ Shrink()

void Shrink ( )

Deletes the rest of the string after the first null character.

§ LowerCase()

void LowerCase ( )

Coverts the string to lower case.

§ UpperCase()

void UpperCase ( )

Coverts the string to upper case.

§ SwapCase()

void SwapCase ( )

Coverts the string to swapped case.

§ TitleCase()

void TitleCase ( )

Coverts the string to title case.

§ GetLowerCase()

String GetLowerCase ( ) const

Returns the lower case version of the string.

§ GetUpperCase()

String GetUpperCase ( ) const

Returns the upper case version of the string.

§ GetSwapCase()

String GetSwapCase ( ) const

Returns the swapped case version of the string.

§ GetTitleCase()

String GetTitleCase ( ) const

Returns the title case version of the string.

§ DecodeFileName() [1/2]

void DecodeFileName ( String path,
String filename 
)

Parses the string as file name string and returns path, filename.

§ DecodeFileName() [2/2]

void DecodeFileName ( String path,
String filename,
String extention 
)

Parses the string as file name string and returns path, filename and extension.

§ LoadFromStream()

int LoadFromStream ( FILE *  stream,
unsigned int  count 
)

Reads 'count' number of characters from the given file stream onto the string. Returns final length of the string.

§ LoadFromFile()

int LoadFromFile ( const String filename)

Loads the string from the file. Returns final length of the string.

§ SaveToStream()

int SaveToStream ( FILE *  stream) const

Writes string data to the given file stream. Returns the number of bytes written.

§ SaveToFile()

int SaveToFile ( const String filename) const

Saves the string to the text file. Returns the number of bytes written.

Friends And Related Function Documentation

§ operator<<

std::ostream& operator<< ( std::ostream &  os,
const String str 
)
friend

Overloaded stream operator. It only works if you include iostream before this file.