Next
Previous
Contents
To use String class, you should first refer to a
sample program "example_String.cpp"
given in
Appendix A
and the String class which is given in
Appendix B.
The
'String class'
is a complete replacement for char and char * datatype.
You can use
'String class'
just like char and get much more functionalities.
You should link with the library 'libString.a' which you can build from the
makefile given in
Appendix H and copy the library to
/usr/lib or /lib directory where all the "C++" libraries are located. To use
the 'libString.a' compile your programs like -
g++ example.cpp -lString
See illustration sample code as given below -
String aa;
aa = " Washington DC is the capital of USA ";
// You can use aa.val() like a 'char *' variable in programs !!
for (unsigned long tmpii = 0; tmpii < aa.length(); tmpii++)
{
//fprintf(stdout, "aa.val()[%ld]=%c ", tmpii, aa.val()[tmpii]);
fprintf(stdout, "aa[%ld]=%c ", tmpii, aa[tmpii]);
}
// Using pointers on 'char *' val ...
for (char *tmpcc = aa.val(); *tmpcc != 0; tmpcc++)
{
fprintf(stdout, "aa.val()=%c ", *tmpcc);
}
The
'String class'
provides these operators :-
- Equal to ==
- Not equal to !=
- Assignment =
- Add to itself and Assignment +=
- String concatenation or addition +
For example to use operators -
String aa;
String bb("Bill Clinton");
aa = "put some value string"; // assignment operator
aa += "add some more"; // Add to itself and assign operator
aa = "My name is" + " Alavoor Vasudevan "; // string cat operator
if (bb == "Bill Clinton") // boolean equal to operator
cout << "bb is eqaul to 'Bill Clinton' " << endl;
if (bb != "Al Gore") // boolean 'not equal' to operator
cout << "bb is not equal to 'Al Gore'" << endl;
The functions provided by String class has the same name
as that Java language's
String class. The function names and the behaviour is exactly same
as that of Java's string class!! This will facilitate portability of code
between Java and C++ (you can cut and paste and do minimum changes to code).
The
'String class'
provides these Java like functions :-
- Current string length length()
- char charAt(int where);
- void getChars(int sourceStart, int sourceEnd, char target, int targetStart);
- char* toCharArray();
- bool equals(String str2); // See also == operator
- bool equals(char *str2); // See also == operator
- bool equalsIgnoreCase(String str2);
- bool regionMatches(int startIndex, String str2, int str2StartIndex, int numChars);
- bool regionMatches(bool ignoreCase, int startIndex, String str2, int str2StartIndex, int numChars);
- String toUpperCase();
- String toLowerCase();
- bool startsWith(String str2);
- bool startsWith(char *str2);
- bool endsWith(String str2);
- bool endsWith(char *str2);
- int compareTo(String str2);
- int compareTo(char *str2);
- int compareToIgnoreCase(String str2);
- int compareToIgnoreCase(char *str2);
- int indexOf(char ch, int startIndex = 0);
- int indexOf(char *str2, int startIndex = 0);
- int indexOf(String str2, int startIndex = 0);
- int lastIndexOf(char ch, int startIndex = 0);
- int lastIndexOf(char *str2, int startIndex = 0);
- int lastIndexOf(String str2, int startIndex = 0);
- String substring(int startIndex, int endIndex = 0);
- String replace(char original, char replacement);
- String replace(char *original, char *replacement);
- String trim(); // See also overloaded trim()
- String concat(String str2); // See also operator +
- String concat(char *str2); // See also operator +
- String append(String str2) {return concat(str2);} // See also operator +
- String append(char *str2) {return concat(str2);} // See also operator +
- String append(int bb) {return (*this + bb);} // See also operator +
- String append(unsigned long bb) {return (*this + bb);} // See also operator +
- String append(float bb) {return (*this + bb);} // See also operator +
- String insert(int index, String str2);
- String insert(int index, char ch);
- String reverse(); // See also overloaded reverse()
- String deleteCharAt(int loc);
- String deleteStr(int startIndex, int endIndex); // Java's "delete()"
These are addional functions which are not available in Java.
- Left trim the string. Remove leading white-spaces - newlines, tabs ltrim()
- Right trim the string. Remove trailing white-spaces - newlines, tabs rtrim()
- Remove trailing and leading white-spaces trim()
- Remove trailing newlines chop()
- Change string to upper case to_upper()
- Change string to lower case to_lower()
- Truncate or round-off the float value roundf(float input_val, short precision)
- Truncate or round-off the double value roundd(double input_val, short precision)
- Find position, matching substr beginning
from start pos(char *substr, unsigned long start)
- Explodes the string and returns the list in the
list-head pointer explodeH explode(char *seperator)
- Implodes the strings in the list-head pointer explodeH and
returns the String variable implode(char *glue)
- Joins the strings in the list-head pointer explodeH and
returns the String variable join(char *glue)
- Repeat the input string n times repeat(char *input, unsigned int multiplier)
- Replace all occurences of string 'needle' with 'str' in
the haystack 'val' replace(char *needle, char *str)
- Translate certain chars str_tr(char *from, char *to)
- Center the text string center(int length, char padchar = ' ')
- Formats the original string by placing 'number' of 'padchar' characters
between each set of blank-delimited words. Leading and Trailing blanks
are always removed. If 'number' is omitted or is 0, then all spaces are
in the string are removed. The default number is 0 and
default padchar ' ' space(int number = 0, char padchar = ' ')
- The result is string comprised of all characters between
and including 'start' and 'end' xrange(char start, char end)
- Removes any characters contained in 'list'. The default character
for 'list' is a blank ' ' compress(char *list)
- Deletes a portion of string of 'length' characters from 'start' position.
If start is greater than the string length than string is
unchanged delstr(int start, int length)
- The 'newstr' in inserted into val beginning at 'start'. The 'newstr' will
be padded or truncated to 'length' characters. The default 'length' is
string length
of newstr insert(char *newstr, int start = 0, int length = 0, char padchar = ' ')
- The result is string of 'length' chars madeup of leftmost chars in val.
Quick way to left justify a string left(int length = 0, char padchar = ' ')
- The result is string of 'length' chars madeup of rightmost chars in val.
Quick way to right justify a string right(int length = 0, char padchar = ' ')
- The 'newstr' in overlayed into val beginning at 'start'. The 'newstr' will
be padded or truncated to 'length' characters. The default 'length' is
string length
of newstr overlay(char *newstr, int start = 0, int length = 0, char padchar = ' ')
- Sub-string, extract a portion of string substr(int start, int length = 0)
- matches first match of regx at(char *regx)
- Returns string before regx before(char *regx)
- Returns string after regx after(char *regx)
- Returns true if string is NULL value bool isnull()
- Resets the string to NULL clear()
Some miscellaneous String functions are given here, but DO NOT USE these,
and instead use operators like '+', '+=', '==' etc.. These are 'private' members of
the 'String' class.
- Copy string str_cpy(char *bb)
- Long integer converted to string str_cpy(unsigned long bb)
- Integer converted to string str_cpy(int bb)
- Float converted to string str_cpy(float bb)
- String concatenate a char * str_cat(char *bb)
- String concatenate a int str_cat(int bb)
- String concatenate a int str_cat(unsigned long bb)
- String concatenate a float str_cat(float bb)
- Is equal to String ? bool equalto(const String & rhs, bool type = false)
- Is equal to char* ? bool equalto(const char *rhs, bool type = false)
For example to convert integer to string do -
String aa;
aa = 34; // The '=' operator will convert int to string
cout << "The value of aa is : " << aa.val() << endl;
aa = 234.878; // The '=' operator will convert float to string
cout << "The value of aa is : " << aa.val() << endl;
aa = 34 + 234.878;
cout << "The value of aa is : " << aa.val() << endl;
// The output aa will be '268.878'
// You must cast String to convert
aa = (String) 34 + " Honourable President Ronald Reagan " + 234.878;
cout << "The value of aa is : " << aa.val() << endl;
// The output aa will be '34 Honourable President Ronald Reagan 234.878'
Next
Previous
Contents