ISO C++ has a string class in the Standard Library. This handout is
not a complete
listing of the facilities of the string class; it describes the most
commonly used member functions. Information on how to create and
initialize strings can be found in the
ISO C++ String Class handout.
Remember, that the first character in a string is in
position 0.
- int string::length() const;
- The length member function returns the
length of the
invoking object. For example:
string saying("all's well that ends well");
cout << "The length of saying is "
<< saying.length() << endl;
The output is:
The length of saying is 25
- char string::at(int) const;
- The at member function returns the character in the
position of the invoking object given by the parm.
For example:
string saying("all's well that ends well");
cout << "The seventh character is "
<< saying.at(6) << endl;
The output is:
The seventh character is w
- string& string::insert(int pos, const string& str);
- The insert member function has two parms: the first is an int,
and the second is a string. insert adds the second parm into the
invoking object
starting in the position given by the first parm.
For example:
string saying("all's well that ends well");
string addition("**hi**");
saying.insert12,(addition);
saying.insert(1,"more");
cout << saying << endl;
The output is:
amorell's well t**hi**hat ends well
- string& string::erase(int pos, int n=npos);
- The erase member function has two parms; the first is an
int for the starting position, the second is an int for the number
of characters to delete.
erase deletes characters from the invoking object. The characters
removed start in the position given by the first parm. The number of
characters removed is given by the second parm. If the second parm is
not specified, all characters from the starting position to the
end of the string are deleted, since the special constant
npos indicates that the rest of the characters in the
string should be deleted. For example:
string saying("all's well that ends well");
saying.erase(2,6);
cout << saying << endl;
saying.erase(16);
cout << saying << endl;
The output is:
alll that ends well
alll that ends w
- string string::substr (int pos, int n) const;
- The substr member function has two parms; the first
in an int for the starting position, the second is an int for the number
of characters to return. substr returns a substring from the
invoking object. The substring starts with the character in the
position given by the first parm, and has length given by the
second parm. The invoking object is unchanged. For example:
string saying("all's well that ends well");
string sub = saying.substr(4,10);
cout << sub << endl;
The output is:
s well tha
- int find (const string& str, int pos=0) const;
int find (char ch, int pos=0) const;
- The find member function searches the invoking object
for the string or character given by the first parm, and returns
the position where the first parm is found. If the second parm is not
specified, the search starts at the beginning of the string. If the
second parm is specified, the search starts in the position given by
the second parm. For example:
string saying("all's well that ends well");
string search("well");
int loc = saying.find(search);
cout << "well is found in position " << loc << endl;
loc = saying.find(search,loc+1)
cout << "well is found again in position "
<< loc << endl;
The output is:
well is found in position 6
well is found again in position 21
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2001