cstr()
Constructor.
~cstr()
Destructor.
cstr(const char * pstr)
Constructor. Creates a string object containing a copy of the specified string.
Parameter | Description |
---|---|
pstr | A 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.
Parameter | Description |
---|---|
pstr | The character string to copy. |
len | The 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.
Parameter | Description |
---|---|
str | The 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.
Parameter | Description |
---|---|
Return value | Pointer 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.
Parameter | Description |
---|---|
Return value | Pointer 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.
Parameter | Description |
---|---|
Return value | The 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.
Parameter | Description |
---|---|
Return value | True 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.
Parameter | Description |
---|---|
pos | Position of the character in the string. |
c | The 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.
Parameter | Description |
---|---|
pos | Position of the first character to be set. |
c | The new character. |
count | The 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.
Parameter | Description |
---|---|
c | The character to fill the string with. |
count | The 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.
Parameter | Description |
---|---|
pstr | The character string to copy. |
len | Max 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.
Parameter | Description |
---|---|
pstr | The character string to append. |
len | The 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.
Parameter | Description |
---|---|
pstr | The zero terminated source string. |
Return value | A 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.
Parameter | Description |
---|---|
str | The source object. |
Return value | A 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.
Parameter | Description |
---|---|
str | The source object. |
Return value | A 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.
Parameter | Description |
---|---|
c | The character to append. |
Return value | A 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();