Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Download Compiler

Final Project

Lab Notes

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
The string Class

Preprocessor directive

   #include <string>

Declaration

    string name;     // creates string object called name

   string name = "character string";     //  itialization

 
Read in an entire line of input

   getline(cin, name);
 
Assignment

      string name, cartoon;
    cartoon = "Fred Flintstone";
    name =  cartoon; // "Fred Flintone" assigned to name
 

Subscript operation

   characters in a string are indexed starting with 0.

   name = "John D";
 
index 0
index1
index2
index 3
index 4
index 5
J
o
h
n

D


      Subscript operator [ ]

      string_object[index]         example:  name[3] = z;
 
Relational operators

   <, >, <=, >=, ==, !=

   Compares characters of two strings in order, until a match occurs.  The value of a character is its ASCII code.
 
Concatenation

    Combines two strings into a single string using + symbol

    string state = "Minnesota", team;

    team = state + " Vikings";   // Assigns "Minnesota Vikings" to team
 
The size() function

   name.size()  - returns the number of characters in the string name
 
The empty()  function

   name.empty() - returns boolean value true if string is empty, false otherwise
 
Substrings

  Substring extraction:

  string_object.substr(first, num_chars)

            first = first character in string to be selected
            num_chars = size to be selected

            substr returns a string object containing the substring.

   string fullName = "Donald Duck", firstName, lastName;
   firstName = fullName.substr(0, 6);
   lastName = fullName.substr(7, 4);
 

   Substring replacement:

   string_object.replace(first, num_chars, replacement)

      fullName.replace(0, 6, "Daisy");  
   // changes fullName to "Daisy Duck"

 

   Substring removal:

      string_object.erase()    
      // makes strin_object an empty string

      string.object.erase(first, num_chars)

       fullName.erase(6, 4);  
    // changes fullname to "Daisy "

 

   Substring insertion:

   string_object.insert(position, new_string)

        fullName.insert(5, " Duck");  // puts " Duck" back into fullName
 

   Substring pattern matching:

   string_object.find(pattern, position)  // look for pattern starting at index position

    find() returns the index (subscript) where the first character of the substring is found.
    If the substring is not found, the function will return string::npos, a special constant (usually -1) .
 
        position = fullName.find("Duck", 0);  // assigns 6 to position

Example:

Find the positions of the character string "duck" in the quote "If it walks like a duck, and quacks like a duck then it must be a duck."

// Find the locations of substrings in a string
# include <iostream>
using namespace std;

# include <string>

int main()
{
   int position;
   string quote, sub;

   cout << "Enter a character string:\n";
   getline(cin, quote);

   cout << "Enter a substring:\n";
   getline(cin, sub);

   position = quote.find(sub, 0);

   while( position != string::npos )
   {
      cout << "String found in position " << position << endl;

      position = quote.find(sub, position + 1);
   }
}
cswanson@exa (~/summer) % g++ str_duck1.cpp
cswanson@exa (~/summer) % a.out
Enter a character string:
If it walks like a duck, and quacks like a duck, then it must be a duck
Enter a substring:
duck
String found in position 19
String found in position 43
String found in position 67 
 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming