cstr()

Constructor.

~cstr()

Destructor.

cstr(const char * pstr)

Constructor. Creates a string object containing a copy of the specified string.

ParameterDescription
pstrA zero terminated character string to copy.

Example:

cstr str("Hello world!");

cstr(const char * pstr, size_t len)

Constructor. Creates a string object containing a copy of the specified string.

ParameterDescription
pstrThe character string to copy.
lenThe number of characters to copy.

Example:

cstr str("Testing cstr!\n",7);
assert(str=="Testing");

cstr(const cstr & str)

Copy Constructor. Creates a copy of a string object.

ParameterDescription
strThe string object to copy.

Example:

cstr str1("Hello world!");
cstr str2(str1);

assert(str1==str2);

operator const char * () const

Type cast. Used to cast a string object variable to a character string pointer. The pointer is only valid as long as the string object is not modified.

ParameterDescription
Return valuePointer to the zero terminated string contained inside the string object.

Example:

cstr str("Hello world!");
const char * pStr = str;

const char * c_str() const

Explicit type cast. Used to cast a string object variable to a character string pointer. The pointer is only valid as long as the string object is not modified.

ParameterDescription
Return valuePointer to the zero terminated string contained inside the string object.

Example:

cstr str("Hello world!");
const char * pStr = str.c_str();

size_t len() const

Get the length of the string.

ParameterDescription
Return valueThe number of characters excluding zero termination.

Example:

cstr str("Testing!");
size_t length = str.len();

assert(length==8);

bool isempty() const

Check if the string is empty.

ParameterDescription
Return valueTrue if the string is empty.

Example:

cstr str;
bool result = str.isempty();

assert(result);

void set(size_t pos, char c)

Replace a character in the string with a new character. This function can not be used to extend the string.

ParameterDescription
posPosition of the character in the string.
cThe new character.

Example:

cstr str("hello world!");
str.set(0,'H');

assert(str[0]=='H');

void set(size_t pos, char c, size_t count)

Replace multiple characters in the string with multiple copies of a single character. The length of the string will not be changed. This function can not be used to extend the string.

ParameterDescription
posPosition of the first character to be set.
cThe new character.
countThe number of characters to set.

Example:

cstr str("Hello");
str.set(1,'X',3);

assert(str=="HXXXo");

void fill(char c, size_t count)

Fill a string with multiple copies of a single character. The string will be extended or truncated as necessary. The string will be exactly count characters long after the call.

ParameterDescription
cThe character to fill the string with.
countThe new string length.

Example:

cstr str("Hello");
str.fill('Y',3);

assert(str=="YYY");

void ncpy(const char * pstr, size_t len)

Copy a limited number of characters from a string to the string object. The copying will stop if a zero termination is found or when len characters have been copied, which ever comes first. The old content of the string object will be deleted.

ParameterDescription
pstrThe character string to copy.
lenMax number of characters to copy.

Example:

cstr str;
str.ncpy("Hello World!",5);

assert(str=="Hello");

void ncat(const char * pstr, size_t len)

Append (concatenate) a limited number of characters from a string to the string object. The operation will stop if a zero termination is found or when len characters have been appended, which ever comes first.

ParameterDescription
pstrThe character string to append.
lenThe number of characters to append.

Example:

cstr str("Hello ");
str.ncat("World!",5);

assert(str=="Hello World");

const cstr & operator=(const char * pstr)

Copy (assign) a zero terminated string to the string object. The old content of the string object will be deleted.

ParameterDescription
pstrThe zero terminated source string.
Return valueA reference to the string object itself.

Example:

cstr str = "Hello World!";

const cstr & operator=(const cstr & str)

Copy (assign) the content of another string object to this string object. The old content of the destination object will be deleted.

ParameterDescription
strThe source object.
Return valueA reference to the destination object.

Example:

cstr str1("Hello World!");
cstr str2 = str1;

const cstr & operator+=(const cstr & str)

Append the content of another string object to this string object.

ParameterDescription
strThe source object.
Return valueA reference to the destination object.

Example:

cstr str("Hello ");
str += "World!";

assert(str=="Hello World!");

const cstr & operator+=(char c)

Append a character to the string object.

ParameterDescription
cThe character to append.
Return valueA reference to the string object itself.

Example:

cstr str("Hello World");
str += '!';

assert(str=="Hello World!");

void clear()

Clear the content of the string object. No memory will be deallocated by this function. Call garb() to release unused memory allocated by the string object.

Example:

cstr str("Hello World");
str.clear();

assert(str.isempty());

void garb()

Release all unused memory allocated by the string object. This function will not modify the content of the string object.

Example:

cstr str("Hello World");
str.garb();