ISO C++ String Class - Additional Functions

ISO C++ has a string class in the Standard Library. This handout describes some of the additional methods in the string class. Some of these functions use a parameter for the position of a character within a string. When using these functions, you must use position zero for the first character in a string.

int string::size()
int string::length()

These functions both do the same thing. They have no parms and return an int which is the number of characters in the string.

string string::insert(int pos,string s)
string string::insert(int p,char s[])

This function inserts the second parm (either a string object or a C-style string) into the invoking object before position p. It also returns a string object with the same contents as the modified invoking object.

string string::append(string s)
string string::append(char s[])
string string::append(char c)

This function concatenates the parameter (either a string object, a C-style string, or a character) to the invoking object. It also returns a string object with the same contents as the modified invoking object.

string string::erase(int p, int n)
This function deletes n characters from the invoking object, starting with the character in position p. It also returns a string object with the same contents as the modified invoking object.

string string::replace(int p,int n,string s)
This function replaces n characters in the invoking object, starting with the character in position p, with the string parameter s. It also returns a string object with the same contents as the modified invoking object.

int string::find(string s)
int string::find(string s,int p)
int string::find(char c)
int string::find(char c,int p)

This function searches for a string s or a char c within the invoking object. The first form and third forms start searching at the beginning of the invoking object; the second and fourth forms start searching at position p of the invoking object. The function returns the position where the string s or the char c was found. If the string or char is not found, the function returns a value greater than the length of the invoking object.

string string::substr(pos)
string string::substr(pos,n)

This function returns a substring of the invoking object. The first form returns a substring starting at position pos and going to the end of the invoking object. The second form returns a substring of length n starting at position pos of the invoking object.

string string::operator+(string s)
string string::operator+(char s[])

This function returns a string which contains the chars in the parm (either a string object or a C-style string) concatenated onto the end of the chars in the invoking object. The parm and the invoking object are not changed.

char& string::operator[](int i)
This function returns the character in location i of the invoking object. It can be used to access the character in that location (Example: char ch = str[i];), or to modify the character in that location (Example: str[i] = 'q';).


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2001