Section 7 - More S-C String Functions

This section will provide you with a variety of string functions that you can apply to s-c strings, i.e., objects of the string class.

1A.7.1 getline()

The easiest way to get a string object from the user is by stating a question with a cout statement, then reading in the results using a getline() method call.

Here is how it works:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    // Declare a string reference to hold the user's answer
    string strUserAnswer;

    // ask the question and get the answer
    cout << "What's your name? ";
    getline(cin, strUserAnswer);

    cout << "Hello " + strUserAnswer + "\n";
    return (0);
}

It's that simple.  When we run that code, it will result in the following :

console shot

There are several things to notice about this source and run:

Although you can use cin >> strUserAnswer this is usually not advisable since the string object will only receive the first word that the user types.   In other words, cin uses whitespace to delimit its input, and does not read anything after the whitespace for a single string variable.

1A.7.2 Other Useful string Functions

THE LENGTH OF A STRING

You can use the length() instance method of the string class to get the length of a string:  strUserAnswer.length() returns the length of the string contained in strUserAnswer.

COPYING STRINGS

You can use the assignment operator, =, to copy one string into another.  NOTE: This only works for s-c strings! CStrings require using strcpy() or strncpy().

string first = "Gunter", temp;

temp = first;    // copies "Gunter" into temp
first = "Grass"; // replaces "Gunter" with "Grass"

COMPARING TWO STRINGS

Use the <, > or == operators to compare two strings:

if ( str1 == str2 )
{
    // do this if they are equal
}
else
{
    // do this if they are NOT equal
}

The less than, <, or greater than, >, operator can be used to determine which of two strings occurs earlier in the alphabetical ordering.

Besides the relational operators, there is a string instance method, compare() that can be used to compare two strings.  It is called simply:

string1.compare(string2);

And returns a negative int if string1 < string2, a positive int if string1 > string2 and zero if string1 == string2.

int result = string1.compare(string2);

if (result==0)
    // they were equal
else if (result < 0)
    // string1 was before string2
else
    // string1 was after string2
NOTE

This only works for s-c strings. CStrings require using strcmp() or strncmp().

LOOKING FOR SUBSTRINGS

If you want to find out if and where a (usually smaller) string (or string literal) is inside a (usually larger) string variable, do this:

p = s1.find(s2);

p will be the int index of the first occurrence of the string s2, inside the string s1. If the string s2 doesn't occur at all, then p will be the static const (defined in class string) string::npos.

EXTRACTING A SUBSTRING

If you want to form a new (usually smaller) string by taking a substring of a (usually larger) string, you need to specify the start position and the length, two int values that will define the new substring:

s2 = s1.substr(start, length);

For example, if s1 = "Hi Mom, Hi Dad!", and s2 = s1.substr(7, 5), then s2 would be " Hi D".

CONVERTING A STRING  TO A DOUBLE OR INT

If you want to convert a string, stringNum1, that contains a number to a double or int variable num1, do this:

#include <sstream>

// then, when needed ...
istringstream(stringNum1) >> num1;


Notice that you have to #include <sstream> when working with istringstream.  This is also true of ostringstream in the next section.

CONVERTING A DOUBLE OR INT TO  A STRING

The other direction requires three statements:

ostringstream cnvrt;
cnvrt << num1;
stringNum1 = cnvrt.str();

This results in any numeric variable having its value turned into a string that can be then stored into a string variable .  Also, if you have to effect a conversion from number to string twice or more in a program, you must   use a separate ostringstream object in place of cnvrt. For instance you could use the name cnvrt2 for the second conversion, cnvrt3 for the third, etc.