C++ String Class Operations

 

To use the string class, #include <string>    (or   #include <string.h>   if not using namespace std).

 

Operators

 

=                          Assign a string constant or variable to a string variable:   string a = "Hi Mom";

              string b;   b = a;   Now b is an exact copy of a

>>                        cin >> b;     Skips leading blanks and then reads chars until a blank or a newline

  is found. If a newline is found, it is not read and remains in the

  input stream.  (also see getline)

 

<<                        cout << b;     Output all characters assigned to the string

 

+                          Produces concatenated expression

              string a;   a = "car";   a = b + "pet"; Now a = "carpet"

 

+=                        Concatenates to target string  (also see append)

                            string a;   a = "car";    a += "pet";     Now a = "carpet"

 

[ ]                         1)  Use a subscript to access a specific char as if the string were an array:

                                    string s = "Hi Mom"; s[0]=='H', s[1]=='i', s[2]==' ', etc.

                            2)  Use a subscript to overwrite an existing char in a string:

                                    string s = "Hi Mom"; s[3] = "D'; s[4] = 'a'; s[5] = 'd';

                                    So now s is "Hi Dad'

                               There is no check for an invalid subscript. (also see at)

 

>     <                   S   tandard boolean comparisons

==   !=                  string a = “car”;  string b = “care”;  string c = “can”;

>=   <=                 if (a == b) …   False: ‘c’ == ‘c’, ‘a’ == ‘a’, ‘r’ == ‘r’, but “care” has more characters

                            if (b < c) …        True because ‘a’ < ‘o’ ; doesn’t matter that “care” is longer or that  ‘r’ > ‘n’

                            if (a > c) …     True because ‘r’ > ‘n’

 

 

Functions

 

constructor        Declare and initialize a string:

             1) string a;                 // a is an empty string with length 0

              2) string f = "AAA";         // f is a string "AAA" with length 3

3) string b ("AAA");         // b is a string "AAA" with length 3

              4) string c (3, 'A');        // c is a string "AAA" with length 3

              5) string d ("ABC", 2);      // d is a string "AB" with length 2

              6) string e ("ABCD", 2, 3); // e is a string "BCD with length 3

 

getline                 getline(cin, s);   

                            Reads all chars (including blanks) from the current cursor/file position until a newline

is found.  The newline is read but is not stored in s.

                            If input is   Computer Science

                            s has a value of  "Computer Science"

                            If same input were read:  cin >> s;   s would only be  "Computer"

 

getline                 getline(cin, s, delim);

Reads all chars (including blanks) from input stream and stores them in s until s.max_size() characters have been read, the end of file occurs, or delim is encountered, in which case delim is read but is not stored in s

 

assign                  1)  a.assign(b);       same as     a = b; 

                            2)  a.assign(b, start, numChars);   b is the string to be copied from, start is

                                 the starting subscript position, numChars is the # of chars to copy    


at                         Allows access to char in a subscript position with range checking:

                            string s = "Hi Mom";    cout << s.at(10);

                            Since 10 is not a valid subscript, the function indicates an error.

                            cout << s[10];  would print garbage or possibly cause a Windows error

 

append                1)  string a = "car";   a.append("pet");  // Now a = "carpet".

 

substsr              Retrieve a substring

                            string a = "Muppets";     cout << a.substr(3, 4);

                            copies chars from a starting with subscript 3 for a length of 4 chars

                            so the value output is the 4 chars   pets

 

swap                   Swaps two strings:  same as  temp = a;  a = b;  b = temp;

                            string a = "one";    string b = "two";    a.swap(b);

                            Now a = "two" and b = "one".

 

size                     Number of chars currently stored in the string

length                  string a = "one";   cout << a.size();  cout<< a.length();

                            Both statements output a value of 3 .

 

find                      Returns the position in the target string where the parameter string begins

                            string a = "I go to the theater often";

                            cout << a.find("the");   outputs 8 (starting position of first occurrence of "the")

 

rfind                    Searches a string in reverse to find the last occurrence of a parameter string

                            string a = "I go to the theater often";

                            cout << a.rfind("the");  outputs 12 (starting position of "the" within "theater")

 

find_first_of       Searches to find the first occurrence of any char in parameter string

                            string a = "beginning";   cout << a.find_first_of("g");

                            Outputs 2 because it finds the first 'g' in position 2

                            cout << a.find_first_of("nei");  outputs 1 because 'e' is in position 1

 

find_last_of          Searches to find the last occurrence of any char in parameter string

                            string a = "beginning";   cout << a.find_last_of("g");

                            Outputs 8 because it finds the last 'g' in position 8

                            cout << a.find_last_of("nei");  outputs 7 because it finds the last 'n' in pos 7

 

find_first_not_of      Searches to find the first char not in the parameter string

                            string a = "beginning";   cout << a.find_first_not_of("abcdeg");

                            Outputs 3 because i is the first char it finds that is not in abcdeg

 

find_last_not_of     Searches in reverse to find the first char not in the parameter string

                            string a = "beginning";   cout << a.find_last_not_of("abcdeg");

                            Outputs 7 because n is the first char from the end that is not in abcdeg

 

erase                   Shortens the string by removing all chars from parameter position to end of string

                            string a = "The river is wide";      a.erase(9);

                            Now a = "The river"

 

replace                Replaces a substring within a string

                            parameters: starting subscript, # chars to replace, replacement string

                            string a = "The river is wide";  

                              a.replace(13, 4, "narrower here");

                            Now a = "The river is narrower here"

 

insert                   Increases a string's length by inserting a substring before parameter position

                            string a = "The river is wide";   a.insert(13, "very ");

                            Now a = "The river is very wide"

 

clear                    Clears the string object to a zero-length value

 

empty                  Returns a bool value that indicates whether the string is empty (true) or has data (false)