C++ Strings -The ANSI Way

C++ Strings -The ANSI Way

By Mike Toms

Overload, 1(3):, August 1993


How many times have you converted a C++ program from one implementation to another and found that the library classes utilised in one do not match the other. There is no easy way to convert from the Borland String to the Microsoft CString or for that matter to Fred Bloggs all singing all dancing MyString class.

The Library Working Group of the ANSI committee have, among other things, proposed a standard string class, that is to form part of the ANSI/ISO standard. I hope that compiler implementers will be quick to implement a string class similar to the proposed one in order to start using it now rather than in 1997/8/? or whenever the standard is eventually completed.

The standardisation of a string class is not a trivial matter. The co-ordination of character and wide character, as well as text, stream and wstring classes is bad enough, but this has to be further co-ordinated with the function library. Further problems exist as to the structure of a string. Should it be like a C 'string' and be guaranteed a zero terminator, or should it be based on the length and a null character be allowed to exist within a string without being a terminator?

One point to consider in discussing the standard proposal, is that it includes exception handling. Until our compilers support this, I will omit exceptions from most articles, unless the articles are about exceptions. (Any takers to write an article on exceptions?)

The string class as proposed will not have implicit conversions to char*, as such conversions are dangerous and can result in both dangling pointers and the violation of class boundary. Instead an explicit conversion member function string::c_str() is to be provided for those times when you absolutely must have a char pointer.

The proposed string class has been criticised for having too many member functions, this is countered with the argument that if the functionality is not provided for by the standard users will resort to their own ingenious and manifold solutions to the problem. This can reduce portability, so reducing the effectiveness of the standard.

Public Members

Syntax: string(void)
Description: Default constructor. Constructs a string of zero length.

Syntax: string(Size_T nb)
Description: Constructs a string of zero length and suggests that the string will not grow to more than nb number of bytes.

Syntax: string (const string& s)
Description: Copy constructor. Creates a string containing a copy of string s.

Syntax: string(const string& s, Size_T nb)
Description: Copy constructor. Creates a string containing a copy of string s and suggests that the string will not grow more than nb number of bytes.

Syntax: string (const char* cp)
Description: Contains a copy of the bytes from the location pointed to by cp thru to the first null character.

Syntax: string(const char* cp, Size_T nb)
Description: Contains a copy of the bytes from the location pointed to by cp thru to the first null character and suggests that the string will not grow more than nb number of bytes.

Syntax: string(char c)
string(signed char c)
string(unsigned char c)
Description: Creates a string containing the character c

Syntax: string(char c, Size_T rpt)
string(signed char c, Size_T rpt)
string(unsigned char c, Size_T rpt)
Description: Creates a string containing the character c repeated rpt times.

Syntax: ~string(void)
Description: Destructor. Frees all resources allocated to this instance.

Syntax: string& operator = (const string& s)
Description: Destroys original string and makes a copy of string s, provided s is not the same instance as the original string.

Syntax: string& assign (const string& s)
Description: Destroys original string and makes a copy of string s, provided s is not the same instance as the original string.

Syntax: string& assign (const string& s, Size_T n)
Description: Destroys original string and makes a copy of n characters of string s, provided s is not the same instance as the original string, in which case the string is truncated to n characters.

Syntax: operator + = (const string& s)
Description: Appends the contents of the string s to the end of the original string.

Syntax: string& append (const string& s)
Description: Appends the contents of the string s to the end of the original string.

Syntax: string& append(const string& s, Size_T nb)
Description: Appends up to nb bytes of the string s to the end of the original string.

Syntax: int compare(const string& s)
Description: Compares the current string to the string s and returns a value of less than zero if the current string is less than the string s, zero is the strings are equal and greater than zero if the current string is greater than string s. (Same as strcmp in ISO C).

Syntax: int compare(const string& s, Size_T nb)
Description: Compares not more than nb bytes of the current string to the string s. The results returned are the same as compare(const string&).

Syntax: string& insert(Size_T bp, const string& s)
Description: Inserts the string s starting at the position bp into the current string.

Syntax: string& insert(Size_T bp, const string& s, Size_T nb)
Description: Inserts upto the first nb bytes of the string s into the current string starting at the position bp.

Syntax: string& remove(Size_T bp)
Description: Removes the characters starting at bp bytes to the end of the string. (Equivalent of BASIC RIGHTS)

Syntax: string& remove(Size_T bp, Size_T nb)
Description: Removes up to nb characters from the string starting at bp bytes.

Syntax: string& replace (Size_T bp, Size_T nb, const string& s)
Description: Removes up to nb characters from the current string starting at bp bytes and replaces them with a copy of the string s.

Syntax: string& replace (Size_T bp, Size_T nbl, const string& s, Size_T nb2)

Description: Removes up to nbl characters from the current string starting at bp bytes and replaces them with up to nb2 bytes of the string s.

Syntax: char get_at(Size_T bp)
Description: returns the character at the position bp bytes.

Syntax: void put_at (Size_T bp, char c)
Description: Replaces the character at position bp with the character c. If the position is I beyond the current endpoint, the character is appended. (Further will cause an exception to be generated).

Syntax: Size_T find (const string& s)
Description: Locates the first occurrence of the string s in the current string. If the string is found the start position is returned.

Syntax: Size_T find (const string& s, Size_T bp)
Description: Locates the first occurrence of the string s after the starting position of bp bytes in the current string. If the string is found, the start position is returned.

Syntax: Size_T rfind(const string& s)
Description: Locates the last occurrence of the string s in the current string. If the string is found, the start position is returned.

Syntax: Size_T rfind(const string& s, Size_T bp)
Description: Locates the last occurrence of the string s in the current string which is not beyond bp bytes of the current string. If the string is found, the start position is returned.

Syntax: string substr(Size_T bp)
Description: Creates a string containing the characters from byte position bp to the end of the string.

Syntax: string substr (Size_T bp, Size_T nb)
Description: Creates a string containing up to nb characters from the current string starting at byte position bp.

Syntax: Size_T find_first_of(const string& s)
Description: Locates the first occurrence in the current string of any of the characters in the string s. If the search was successful, returns the character position within the current string.

Syntax: Size_T find_first_of(const string& s, Size_T bp)
Description: Locates the first occurrence in the current string of any of the characters in the string s starting at byte position bp in the current string. If the search was successful, returns the character position within the current string.

Syntax: Size_T find_first_not_of(const string& s)
Description: Locates the first occurrence in the current string of a character which is not present in the string s. If successful, returns the byte position in the current string at which the character was located.

Syntax: Size_T find_first_not_of(const string& s, Size_T bp)
Description: Locates the first occurrence after the byte position bp in the current string of a character which is not present in the string s. If successful, returns the byte position in the current string at which the character was located.

Syntax: Size_T find_last_of(const string& s)
Description: Locates the last occurrence in the current string of any of the characters in the string s. If the search was successful, returns the character position within the current string.

Syntax: Size_T find_last_of(const string& s, Size_T bp)
Description: Locates the last occurrence in the current string of any of the characters in the string s not beyond byte position bp in the current string. If the search was successful, returns the character position within the current string.

Syntax: Size_T find_last_not_of(const string& s)
Description: Locates the last occurrence in the current string of a character which is not present in the string s. If successful, returns the byte position in the current string at which the character was located.

Syntax: Size_T find_last_not_of(const string& s, Size_T bp)
Description: Locates the last occurrence not beyond byte position bp in the current string of a character which is not present in the string s. If successful, returns the byte position in the current string at which the character was located.

Syntax: Size_T length()
Description: Returns the length of string. This might be greater than strlen(cstr()) if null characters are being stored.

Syntax: Size_T copy (char* ca, Size_T nb)
Description: Copies up to nb characters to the character array pointed to by ca. Returns the number of characters actually copied.

Syntax: Size_T copy (char* ca, Size_T nb, Size_T bp)
Description: Copies up to nb characters starting at byte position bp into the character array pointed to by ca. Returns the number of characters actually copied.

Syntax: const char* c_str()
Description: Returns a pointer to a zero-terminated char array which contains the same character sequence of characters as the current string.

Syntax: Size_T reserve()
Description: Returns the internal storage size of the string. This should always be equal to, or greater than length().

Syntax: void reserve(Size_T nb)
Description: Indicates to the compiler that the string might eventually require nb bytes in length.

Global Functions

Syntax: string operator + (const string& s1, const string& s2)
Description: Concatenates the strings s1 and s2. Returns a new string.

Syntax: int operator == (const string& s1, const string& s2)
int operator != (const string& s1, const string& s2)
Description: Tests for equality or inequality between two strings. Returns 1 (condition true) or 0 (condition false).

Syntax: ostream& operator << (ostream& os, const string& s)
Description: Acts the same as the ostream& operator << (ostream&, char*) operator, except that it will not stop writing when encountering a null character, but to continue for the length() of the string.

Syntax: istream& operator >> (istream& is, string& s)
Description: Acts the same as the normal char* version, except the result is stored in string s.

Syntax: istream& getline(istream& is, string& s)
Description: Acts the same as the normal char* version, except the result is stored in string s.

Syntax: istream& getline(istream& is, string& s, char c)
Description: Acts the same as the normal char* version, except the result is stored in string s.

As you can see from the list, there are no great surprises, and from a simplistic view should not be to difficult to implement. I am looking for a volunteer to take on this task; but don't start until I know, as the lucky volunteer will need a copy of the ANSI proposal to work from.

I feel that this project will benefit many members of the ACCU if we can complete it quickly.






Your Privacy

By clicking "Accept Non-Essential Cookies" you agree ACCU can store non-essential cookies on your device and disclose information in accordance with our Privacy Policy and Cookie Policy.

Current Setting: Non-Essential Cookies REJECTED


By clicking "Include Third Party Content" you agree ACCU can forward your IP address to third-party sites (such as YouTube) to enhance the information presented on this site, and that third-party sites may store cookies on your device.

Current Setting: Third Party Content EXCLUDED



Settings can be changed at any time from the Cookie Policy page.