The characters in a string are numbered, starting at 0 -- don't ask why!
So string("12345") has the character "1" in place 0, "2" in place 1, and so on.
These notes give a simplified description of the ways they can be used.
Include this library
The easy way to use strings is to put these lines at the start of your program
using namespace std;
string newVariable = "initial value";
string newVariable = anyStringExpression;
This is also use to construct a C++ string from an old C char*:
char * cp = "example";
Accessing data about a string == length and empty
If you have a string called s then
s.length()is the number of characters in the string, and
s.empty()is true if the string is empty. So, for example,
string("example").length()has value 7.
Accessing the old-style C char* in a string
This expression gives you the array of chars,
terminated by a null '\0' char:
Concatenating two or more strings
You can use the "+" symbol to combine strings. For example,
if we declare
string a = "abra";
string b = "ca";
string c = "dabra";then
cout << a + b + c <<endl;outputs "abracadabra". Notice you have to have at least one string variable or explicitly convert a old-style C string like "abra" into a modern string:
cout << string("abra") + "ca" + "dabra" <<endl;otherwise C++ has problems with it.
Copying and assigning strings
The normal assignment (=) works with strings.
string substr (int position, int length)extracts a substring. So
string("example").substr (0, 4)is string("exam") and
string("example"). substr (6, 1)is string("e").
By the way.... i first wrote "substr(7,1)" becasue I started counting the characters with "1" not "0". Look out for this "off by one" error.
Searching for a substring
There are many different ways of searching a string for a substring.
The simplest is find.
string("example").find("e")has value 0 (the place of the first "e", and
string("example").find("e", 1)has value 6 -- the next "e" starting from the "x".
int find (substring)
int find (substring, startingPoint)
If a matching substring is not found "find()" returns string::npos which is a value that is never the palce of a substring in a string.
Lexographic/alphabetical) -- same syntax as for ints and doubles.
s1 == s2
s1 != s2
s1 < s2
s1 > s2
Again one of the strings must be modern C++ string. "bad" < "cad" does not compare the contents of the literals.
Input words into a string
This stops at the first whitespace character but does not store it.
cin >> word;
getline (cin, line);This works with all input streams.
cout << stringexpression ...
Working on a character in a string
If s is a string and i is an positive integer that is less than s.lenght()
s[i]is a character in the string and
s.substr(i,1)is a string containing the i'th character in s.
Note: the characters are numbered 0,1,2,3,..... For example,
exampleis 'x' not 'e'. Similarly example is an 'e' and the last valid character.
Warning: bad things happen if you go outsude the range 0,1,2,.... ,length()-1.
Single characters are typed with single quotes
For historical reasons, 'x' is a character and "x" is a string of two characters!
Working on each character in a string in turn
Here is an example, that replaces each 'a' in a string s by 1:
for(int i = 0; i < s.length(); i++)
if( s[i] == 'a' )
s[i] = '1';
Here is a way to use a for loop to output a string backwards
for(int i = 0; i < s.length(); i++)
cout << s[s.length()-i-1];
}Exercise: try running this fragment by hand with string("abcd").
I tested the code in [ ../cs201/back.cpp ]
Exercis: can you right the for loop that starts at the end of the string and works back to 0 (and no further)?
Inputting one character at a time
You can read data from input one character at a time and put it
in a string if you want. Here is a sample
[ ../cs201/backwards.cpp ]
of using cin.get(c).
How can I convert numbers to strings and strings to numbers
I've dug up a couple of advanced types of objects "stringstreams"
that do the job if you need it in a project, for example. Here
[ ../cs201/stringnumbers.cpp ]
is a program that demonstrates how to use
a couple of functions "convertDouble" and
"convertToDouble" that are defined in
[ ../cs201/stringnumbers.h ]
that you can download and #include in any of your programs.
string convertDouble(double d)
s << d;
double convertToDouble(string d)
s >> result;
Stringstreams are a very powerful feature. Suppose you have a
line that describes a person. It has the first name, their
second name, there phone number, and their age. Each
is a field with no spaces. Between them is one or more spaces:
John Doe 909-883-1234 31
Jane Row 714-123-4567 21Then we can write:
string first, second, phone; int age;
data >> first >> second >> phone >> age;to unpack the data.
KISS file handling
Here is an example of the kind of simple code you can use that
combines stringstreams, objects, etc.
[ ../cs201/Widget.h ]
[ ../cs201/listWidgets.cpp ]
. . . . . . . . . ( end of section The C++ Standard string library) <<Contents | End>>