| Member function name | Description |
|---|---|
s.append(str) |
add text to the end of a string |
s.compare(str) |
return <0, 0, or >0 depending on relative ordering |
s.erase(index, length) |
delete text from a string starting at given index |
s.find(str), s.rfind(str) |
first or last index where the start of str appears in this string (returns string::npos if not found). |
s.insert(index, str) |
add text into a string at a given index. |
s.length() or s.size() |
number of characters in this string. |
s.replace(index, len, str) |
replaces len chars at given index with new text. |
s.substr(start, length) or s.substr(start) |
the next length characters beginning at start (inclusive); if length omitted, graps till end of string. |
string name = "Thomas Edison";
if (name.find("Edi") != string::npos) {
name.erase(7, 6); //"Thomas" remains
}
+ or +=:
string s1 = "Nik"; s1 += "ola"; //Nikola
string s2 = "Tesla"; // ==, !=, <, <=, >, >=
if (s1 < s2 && s2 != "abc") { //true
...
}
s1.append(" Tesla"); //"Nikola Tesla"
s1.erase(3, 3); //"Nik Tesla"
s1[8] = '@'; //"Nik Tesl@"
| function name | Description |
|---|---|
endsWith(str, suffix), startsWith(str, suffix) |
true if str ends or begins with the given text suffix |
integerToString(int), realToString(double), stringToInteger(str), stringToReal(str) |
convert between numbers and strings |
equalsIgnoreCase(s1, s2) |
true if s1 and s2 have same chars, ignoring casing |
toLowerCase(str), toUpperCase(str) |
returns an upper/lowercase version of a string. |
trim(str) |
return string with surrounding whitespace removed. |
if (startsWith(name, "Mr. ")) {
name += integerToString(age) + " years old";
}
Exercise: implement endsWith() using rfind() and length().
Exercise: implement endsWith() using substr() and length().
Exercise: what's the output?
void mystery(string a, string &b)
{
a.erase(0, 1); // erase 1 from index 0
b += a[0];
b.insert(3, "FOO"); //insert at index 3
}
int main()
{
string a = "nikola";
string b = "tesla";
mystery(a, b);
cout << a << " " << b << endl;
return 0;
}
What is printed?
nikola tesFOOlai
String exercise: write a function nameDiamond that accepts a string parameter and prints its letters in a "diamond" format as shown below. For example, nameDiamond("TESLA") should print:
T
TE
TES
TESL
TESLA
ESLA
SLA
LA
A
Solution
void nameDiamond(string s)
{
int len = s.length();
//print top half of diamond
for (int i = 1; i <= len; i++) {
cout << s.substr(0, i) << endl;
}
//print bottom half of diamond
for (int i = 1; i < len; i++) {
for (int j = 0; j < i; j++) { //indent
cout << " "; // with spaces
}
cout << s.substr(i, len - i) << endl;
}
}
Another solution
void nameDiamond(string s)
{
int len = s.length();
//print top half of diamond
for (int i = 1; i <= len; i++) {
cout << s.substr(0, i) << endl;
}
//print bottom half of diamond
for (int i = 1; i < len; i++) {
s.replace(i, 1, " ");
cout << s.substr(i, len - i) << endl;
}
}
Question: Will this solution work if the parameter s was pass-by-reference? What if s was a const reference parameter?
"hi there" is a C string!
length, find, or operators.+. C-string + char/int produces garbage, not "hi?" or "hi41". The bug usually manifests in print statements and you will see partial strings.
n to the memory address of the C string "42"!!string s = "hi"; //convert to C++ string s += "there";
Similarly:
string s = "hi"; //convert to C++ string s += '?';Works because of auto-conversion from C string to C++ string in
string a = ....
For appending integers to strings:
s += integerToString(41); //"hi?41"
Another example:
int n = stringToInteger("42"); //42
Explicit string-int conversion using Stanford library.