|
| <string> - a reference |
<string> class
- Required header file:
#include <string>
using namespace std;
- The operators, =, ==, >, >=, <, <=, != +, +=, [i] work with strings.
= copies one string into another (similar to strcpy in C)
== tests if two strings have the same characters (case sensitive)
+ concatenates two strings together, creating a new string
+= appends the right string onto the end of the left string
string[i] gets the character at index number i in the string
stringA = "any string literal";
stringA = stringB;
// also you can write:
stringA.assign(stringB);
stringC[2] = 'r';
if(stringD == stringE)
cout << "strings match!" << endl;
if(stringD > stringE)
cout << "stringD lexigraphically greater than stringE"
<< endl;
stringF = stringG + stringH;
stringI += "any string literal";
stringI += stringK;
// also you can write:
stringI.append("stringK");
- Constructors:
string stringA; // creates an empty string
string stringB("initial string");
string stringC(stringB); // copy constructor
string stringD = "initial string";
string stringE(a_c_string);
string stringF(n, c); // where c is a character and
// n is the number of times
// c is repeated.
- Size and length:
stringA.size() // returns the number of characters (int)
stringA.length() // exactly the same as stringA.size()
stringB.empty() // returns if string is 0 length (bool)
- C_strings from C++ strings:
char* a_c_string_pointer;
a_c_string_pointer = stringA.c_str();
char a_c_string[256];
stringA.copy(a_c_string, n); // copy into a_c_string
stringA.copy(a_c_string, n, i); // n -- number of chars
// i -- starting index
- Inserting another string at a specific index (size of stringA grows):
stringA.insert(i, a_cpp_string); // i -- starting index
stringA.insert(i, a_c_string);
- Replacing with another string at a specific index (size of stringA remains the same):
stringA.replace(i, m, a_cpp_string); // i -- start index
stringA.replace(i, m, a_c_string); // m -- max number
// of chars replaced
- Erasing characters:
stringA.clear(); // erase all characters. stringA is empty.
stringA.erase(); // same as stringA.clear()
stringA.erase(i); // erase all characters starting at index i
stringA.erase(i, n); // erase n characters starting at i
- Extracting a substring (all functions return a string):
substringA = stringA.substr(i); // starting at index i
substringA = stringA.substr(i, n);// n characters long,
// starting at index i
- Finding a whole substring (all functions return the integer index of substring found --
They return string::npos if the substring is not found)if ( (index = stringA.find(substring)) == string::npos)
cout << "substring not found" << endl;
else
cout << "substring found at index " << index << endl;
index = stringA.find(c); // c is a character
index = stringA.rfind(c); // search backwards
index = stringA.find(substring); // substring is a string
index = stringA.rfind(substring); // search backwards
index = stringA.find(c_string); // c_string is char*
index = stringA.rfind(c_string); // search backwards
index = stringA.find(c, i); // start at index i
index = stringA.rfind(c, i); // start at i, search
// forwards
index = stringA.find(substring, i);
index = stringA.rfind(substring, i);
index = stringA.find(c_string, i);
index = stringA.rfind(c_string, i);
index = stringA.find(c, i, n); // search at most n chars
index = stringA.rfind(c, i, n);
index = stringA.find(substring, i, n);
index = stringA.rfind(substring, i, n);
index = stringA.find(c_string, i, n);
index = stringA.rfind(c_string, i, n);
- Find the first or last character that is contained in another string (returns the index --
or string::npos if none of the characters is found)if ( (index = stringA.find_first_of(other)) == string::npos)
cout << "no characters from other string found" << endl;
else
cout << "first character found at index " << index << endl;
index = stringA.find_first_of(c); // c is a character
index = stringA.find_last_of(c);
index = stringA.find_first_of(other); // other is a string
index = stringA.find_last_of(other);
index = stringA.find_first_of(c_string); // c_string is char*
index = stringA.find_last_of(c_string);
index = stringA.find_first_of(c, i); // start at index i
index = stringA.find_last_of(c, i);
index = stringA.find_first_of(other, i);
index = stringA.find_last_of(other, i);
index = stringA.find_first_of(c_string, i);
index = stringA.find_last_of(c_string, i);
- Find the first or last character that is absent from another string (returns the index --
or string::npos if one of the characters is found)if ((index=stringA.find_first_not_of(other)) == string::npos)
cout << "all characters in stringA are in other string";
else
cout << "first character not found";
index = stringA.find_first_not_of(c); // c is a character
index = stringA.find_last_not_of(c);
index = stringA.find_first_not_of(other);// other is a string
index = stringA.find_last_not_of(other);
index = stringA.find_first_not_of(c_string);// char* c_string
index = stringA.find_last_not_of(c_string);
index = stringA.find_first_not_of(c, i); // start at index i
index = stringA.find_last_not_of(c, i);
index = stringA.find_first_not_of(other, i);
index = stringA.find_last_not_of(other, i);
index = stringA.find_first_not_of(c_string, i);
index = stringA.find_last_not_of(c_string, i);
More Information:
Deitel and Deitel, Chapter 16.
| Course Pages: Csc 123, Csc 125 |
| Scott Badman Office: B132 Phone: 353-2250 sbadman@parkland.edu |
Parkland College, 2400 W. Bradley Avenue, Champaign, IL 61821 |