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 ()
 Default constructor.
 
 String (String const &src)
 Copy constructor.
 
 String (char const *src)
 Sets the string using the given array.
 
 String (char src)
 Sets the string using the given number.
 
 String (int src)
 Default constructor.
 
 String (long src)
 Default constructor.
 
 String (float src)
 Default constructor.
 
 String (double src)
 Default constructor.
 
 String (char const *src, unsigned int count)
 Sets the string using the first 'count' characters of the given array.
 
 String (double src, int digits, int precisition)
 Sets the string as the given double number 'src' using given number of digits and precision.
 
 String (int size, char const *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.
 
virtual ~String ()
 Destructor.
 
Set methods (Assign value)
StringSet (String const &str)
 Sets the string using the given string. Returns the string.
 
StringSet (char const *src)
 Sets the string using the given array. Returns the string.
 
StringSet (char src)
 Sets the string as the given character. Returns the string.
 
StringSet (int src)
 Sets the string using the given number. Returns the string.
 
StringSet (long src)
 Sets the string using the given number. Returns the string.
 
StringSet (float src)
 Sets the string using the given number. Returns the string.
 
StringSet (double src)
 Sets the string using the given number. Returns the string.
 
StringSet (char const *src, unsigned int count)
 Sets the string using the first 'count' characters of the given array. Return this string.
 
StringSet (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.
 
StringSet (int size, char const *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.
 
Assignment operators
Stringoperator= (String const &src)
 
Stringoperator= (char const *src)
 
Stringoperator= (char src)
 
Stringoperator= (int src)
 
Stringoperator= (long src)
 
Stringoperator= (float src)
 
Stringoperator= (double src)
 
Editing methods
void EmptyString ()
 Converts to empty string.
 
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.
 
void SetCharString (char chr, int count)
 Creates a string of length 'count' with the given character 'chr'.
 
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.
 
void Insert (String const &str, int index)
 Inserts the given string to the given index.
 
int DeleteCarriageReturns ()
 Deletes all carriage return characters. Returns the number of characters deleted.
 
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.
 
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.
 
int DeleteChars (String const &set)
 Deletes all the characters in the string that exits in the given set. Returns the number of characters deleted.
 
void Reverse ()
 Reverses the string.
 
void SwapChars (int index1, int index2, int count=1)
 Swap 'count' number of characters starting from 'index1' and 'index2'.
 
StringFormat (int size, char const *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.
 
char & operator[] (int index)
 Returns the character of the string at the specified index.
 
Get value methods
int Length () const
 Returns the length of the string.
 
char const * GetString () const
 Returns the char array that keep the string data.
 
int IsEmpty () const
 Checks if the string is empty (length is zero).
 
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.
 
int SearchCharInSet (int index, String const &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.
 
int SearchSet (String const &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.
 
int ReverseSearchSet (String const &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.
 
int SearchNextSet (int start, String const &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.
 
int GetPosition (char c) const
 Returns the first position of the character. If the character is not found, returns -1.
 
int GetPosition (String const &str) const
 Returns the first position of the given string. If the string is not found, returns -1.
 
int GetLastPosition (char c) const
 Returns the last position of the character. If the character is not found, returns -1.
 
int GetLastPosition (String const &str) const
 Returns the last position of the given string. If the string is not found, returns -1.
 
int GetNextPosition (int start, char c) const
 Returns the next position of the character. If the character is not found, returns -1.
 
int GetNextPosition (int start, String const &str) const
 Returns the next position of the given string. If the string is not found, returns -1.
 
int GetLineNumber (int pos) const
 Returns the line number of the given position in the string.
 
int GetNumLines () const
 Returns the number of lines in the string.
 
int CountChar (char c) const
 Returns the number of characters in the string that are equal to the given character 'c'.
 
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.
 
int CountChars (String const &set) const
 Returns the number of characters in the string that exists in the given character set.
 
char operator[] (int index) const
 Returns the character of the string at the specified index.
 
Comparison methods
int operator== (String const &str) const
 Checks if this string is the same as 'str'.
 
int operator!= (String const &str) const
 Checks if this string is not the same as 'str'.
 
int operator< (String const &str) const
 Checks if this string is smaller than 'str'.
 
int operator<= (String const &str) const
 Checks if this string is smaller than or equal to 'str'.
 
int operator> (String const &str) const
 Checks if this string is greater than 'str'.
 
int operator>= (String const &str) const
 Checks if this string is greater than or equal to 'str'.
 
int Compare (String const &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'.
 
int Compare (String const &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'.
 
int CompareIC (String const &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'.
 
int CompareIC (String const &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'.
 
int IsCapitalChar (int pos) const
 Returns if the char in the given position is a capital letter.
 
int IsLowerCaseChar (int pos) const
 Returns if the char in the given position is a lower case letter.
 
int IsNumericChar (int pos) const
 Returns if the char in the given position is a numeric character.
 
int IsAlphaChar (int pos) const
 Returns if the char in the given position is a lower case or capital letter.
 
int IsAlphaNumericChar (int pos) const
 Returns if the char in the given position is a lower case or capital letter or a numeric character.
 
int IsNameChar (int pos) const
 Returns if the char in the given position is a lower case or capital letter, a numeric character or '_'.
 
int IsSpaceChar (int pos) const
 Returns if the char in the given position is a space character.
 
int IsControlChar (int pos) const
 Returns if the char in the given position is a control char (including new line etc.)
 
Append and substring
void Append (String const &str)
 Appends to the end of the string.
 
Stringoperator+= (String const &str)
 Appends to the end of the string and returns this string.
 
String operator+ (String const &right)
 Appends two strings and returns the result.
 
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.
 
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.
 
Conversion to numbers and number arrays
int ToInt () const
 Converts the string to int.
 
long ToLong () const
 Converts the string to long.
 
float ToFloat () const
 Converts the string to float.
 
double ToDouble () const
 Converts the string to double.
 
void ToIntArray (int n, int *v) const
 Converts the string (ex: "23,43,14") to int array.
 
void ToLongArray (int n, long *v) const
 Converts the string to long.
 
void ToFloatArray (int n, float *v) const
 Converts the string (ex: "23,43,14") to float array.
 
void ToDoubleArray (int n, double *v) const
 Converts the string (ex: "23,43,14") to double array.
 
Trim and shrink
void Trim ()
 Deletes empty characters from the beginning and end of the string.
 
void TrimLeft ()
 Deletes empty characters from the beginning of the string.
 
void TrimRight ()
 Deletes empty characters from the end of the string.
 
void Shrink ()
 Deletes the rest of the string after the first null character.
 
Case change
void LowerCase ()
 Coverts the string to lower case.
 
void UpperCase ()
 Coverts the string to upper case.
 
void SwapCase ()
 Coverts the string to swapped case.
 
void TitleCase ()
 Coverts the string to title case.
 
String GetLowerCase () const
 Returns the lower case version of the string.
 
String GetUpperCase () const
 Returns the upper case version of the string.
 
String GetSwapCase () const
 Returns the swapped case version of the string.
 
String GetTitleCase () const
 Returns the title case version of the string.
 

Friends

std::ostream & operator<< (std::ostream &os, String const &str)
 Overloaded stream operator. It only works if you include iostream before this file.
 

Load/save and file name methods

void DecodeFileName (String &path, String &filename)
 Parses the string as file name string and returns path, filename.
 
void DecodeFileName (String &path, String &filename, String &extention)
 Parses the string as file name string and returns path, filename and extension.
 
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.
 
int LoadFromFile (String const &filename)
 Loads the string from the file. Returns final length of the string.
 
int SaveToStream (FILE *stream) const
 Writes string data to the given file stream. Returns the number of bytes written.
 
int SaveToFile (String const &filename) const
 Saves the string to the text file. Returns the number of bytes written.