The
mychar class
is a complete replacement for char and char * datatype.
You can use
mychar class
just like char and get much more functionalities. See illustration
code as given below -
mychar aa;
aa = " Washington DC is 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]);
}
// Using pointers on 'char *' val ...
for (; *aa.val != 0; aa.val++)
{
fprintf(stdout, "aa.val=%c ", *aa.val);
}
A complete
example program "mychar.cpp" implementing the
mychar class is given in
Appendix A
and mychar class is given in
Appendix B.
The
mychar
class provides these operators :-
- Equal to ==
- Not equal to !=
- Assignment =
- Add to itself and Assignment +=
- String concatenation or addition +
For example to use operators -
mychar aa;
aa = "put some value string";
aa += "add some more";
aa = "My name is" + " Alavoor Vasudevan ";
if (aa == "some string")
cout << "strings are equal" << endl;
if (aa != "some string")
cout << "strings are not equal" << endl;
The
mychar
class provides these functions :-
- Current string length length()
- 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()
- 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 mychar variable implode(char *glue)
- Joins the strings in the list-head pointer explodeH and
returns the mychar variable join(char *glue)
- Repeat the input string n times repeat(char *input, unsigned int multiplier)
- Reverse the string characters reverse()
- 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 mychar functions are given here, but DO NOT USE these,
and instead use operators like '+', '+=', '==' etc..
- 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)
- str_cat(char *bb)
- str_cat(int bb)
- str_cat(unsigned long bb)
- str_cat(float bb)
- bool equalto(const mychar & rhs, bool type = false)
- bool equalto(const char *rhs, bool type = false)
For example to convert integer to string do -
mychar 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 mychar to convert
aa = (mychar) 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'