Go to the first, previous, next, last section, table of contents.


CNString -- Character String

SYNOPSIS

#include <CNCL/String.h>

TYPE

CN_STRING

BASE CLASSES

CNObject

DERIVED CLASSES

None

RELATED CLASSES

CNNamed

DESCRIPTION

CNString is a dynamic string manipulating class. Each CNString manages the string itself (stored as a normal C character string), the length of the string, and the allocated space. The characters are indexed from 0 to length-1.

Constructors:

CNString();
CNString(int extra);
CNString(char c);
CNString(char c, int extra);
CNString(const char* cs);
CNString(const char* cs, int extra);
CNString(const CNString& s);
CNString(const CNString& s, int extra);
CNString(CNParam *param);
Initializes the CNString. The extra parameter gives a hint as to how many extra characters one expects the string to grow. Its default value is 10.

Destructors:

~CNString();
Deletes the CNString.

In addition to the member functions required by CNCL, CNString provides:

default_extra = 10;
Default amount of additional storage allocated. Actually set to 10.
void resize(unsigned i);
Changes the capacity of the string either to its length plus default_extra or to i, whichever is longer.
void to_lower();
Converts all characters in this string to lowercase.
void to_upper();
Converts all characters in this string to uppercase.
void capitalize();
Converts each first character of a word to uppercase, all other characters to lowercase. A new word is either at the beginning of the string or it starts with a space (e.g. "a.R fd" will be capitalized to "A.r Fd").
void strip_crlf();
Removes '\r' and/or '\n' at end of string.
void strip_lspace();
void strip_rspace();
void strip_space();
Removes the left, the right or all white spaces of the string.
unsigned capacity() const;
Returns the amount of space allocated to this string.
unsigned length() const;
Returns the length of this string.
CNString after(unsigned pos, unsigned l = 0) const;
Returns the l characters of this string after position pos (zero-based) as a string. The character at pos will be the first character of the new string. The default value l = 0 will return all characters after pos.
CNString before(unsigned pos, unsigned l = 0) const;
Returns the l characters of this string before position pos (zero based) as a string. The character at pos will be the last character of the new string. The default value l = 0 will return all characters before pos.
CNString& add(char c);
CNString& add(const char* cs);
Adds the character c or the character string cs to the end of this string. If this string does not have enough space allocated it will be changed automatically.
CNString& del(unsigned pos = 0, unsigned l = 1);
Deletes l characters beginning at position pos (zero based). Default values are pos = 0 and l = 1 (e.g. a.del() will delete the first character of string a). If l is set to 0 all characters beginning at pos will be deleted (e.g. a.del(0,0) will delete all characters in string a). Negative pos values count from end of string.
CNString& del(char c, int pos = 0);
CNString& del(const char* cs, int pos = 0);
CNString& del(const CNString& s, int pos = 0);
Deletes character c, character string cs or string s at its first occurence in this string after position pos (zero based, default value pos = 0). It deletes nothing if c, cs or s is not in.
CNString& insert(char c, int pos = 0);
CNString& insert(const char* cs, int pos =0);
CNString& insert(const CNString& s, int pos = 0);
Inserts character c, character string cs or CNString s at position pos (zero based, default value pos = 0).
CNString& replace(char c, int pos = 0);
Replaces the character at position pos (zero based, default value pos = 0) with the character c.
CNString& replace(const CNString& s, int pos = 0, int l = 0);
Replaces the l characters starting at position pos (zero based, default value pos = 0) with the characters of string s. Default value l = 0 will replace as many characters as s.length(). The capacity is changed to the needed space plus the default extra value.
CNString& replace(char oldc, char newc, int pos = 0);
Replaces character oldc at its first occurence after position pos (zero based, default value pos = 0) with newc. If oldc is not in this string after pos nothing will be changed.
CNString& replace(const CNString& olds, const CNString& news, int pos = 0);
Replaces string olds at its first occurence after position pos (zero based, default value pos = 0) with string news. If olds is not in this string nothing will be changed.
int downsearch(char c, int pos = 0);
int downsearch(const CNString& s, int pos = 0);
int downsearch(const char *cs, int pos = 0);
Returns the last occurence of character c or string s in this string before position pos (zero based, default value pos = 0) or the end of this string if pos = 0 or pos >= len. If c or s is not in this string before pos len is returned.
int upsearch(char c, int pos = 0);
int upsearch(const CNString& s, int pos = 0);
int upsearch(const char *s, int pos=0) const;
Returns the first occurence of character c or string s in this string after position pos (zero based, default value pos = 0). Returns len if c or s is not in this string after pos.
bool matches(const char* cs, unsigned pos = 0);
bool matches(const CNString& s, unsigned pos = 0);
Returns TRUE if the character string cs or the string s is equal to the part of this string starting at position pos (zero based, default value pos = 0).
operator const char * () const;
Returns the the C string component of CNString to allow the use of CNStrings in a char * context.
char operator ()(int i) const;
char &operator [](int i);
Returns the i-th character (zero based) of this string.
void operator = (const CNString& s);
void operator = (const char* cs);
void operator = (char c);
Replaces this string with a copy of string s, character string cs or character c.
friend CNString operator + (const CNString& a, const CNString& b);
Returns a string that is the concatenation of the strings a and b.
CNString& operator +=(const CNString &s);
CNString& operator +=(const char* cs);
Appends the C string s or the CNString cs to the end of this string.
friend bool operator < (const CNString& a, const CNString& b);
friend bool operator > (const CNString& a, const CNString& b);
friend bool operator >= (const CNString& a, const CNString& b);
friend bool operator <= (const CNString& a, const CNString& b);
friend bool operator == (const CNString& a, const CNString& b);
friend bool operator != (const CNString& a, const CNString& b);
Returns TRUE if the relation holds between the strings.
void icopy(istream& strm = cin);
istream &operator >> (istream& strm, CNString& s);
Reads all characters up to (not including) the next newline from input stream strm into the string.

For programming convenience, a type for a const reference to a string is provided:

typedef const CNString & CNStringR;


Go to the first, previous, next, last section, table of contents.