C++ Strings
#include <string>
the old, array-type strings are in cstring. string.h
gives you cstring, not the newer C++ string
string s;
cin >> s; terminates with white
space.
getline( in, s); terminates
with newline '\n'
getline(
in, s, term); terminates with the character term.
-
in can be an istream or an ifstream
-
s is a string
-
terminator is removed from input stream but not
put on the string
string t;
t=s; assignment
string u;
u.assign(s, start, numberOfChars);
-
copies the part of s, starting from subscript start, for a total of
numberOfChars characters.
-
s is string to be copied,
-
start is the starting subscript,
-
numberOfChars is the number of characters to copy.
+
-
concatenation (glue two strings together)
s+=u, s.append(u)
-
appends u to the end of s.
s.compare(u)
-
returns 0 if the strings s and u are equivalent lexicographically
-
negative number if s is lexicographically before u
-
positive number if s is lexicographically after u
s.length(), s.size()
-
gives the number of characters (doesn't count the null terminator)
s.substr(start, numberOfChars)
-
returns the substring of s, starting at subscript start, for a total
of numberOfChars characters
-
start is the starting subscript,
-
numberOfChars is the number of characters to take.
-
start must be less than s.length()
-
if numberOfChars is too big, just takes to the end of the string
string::npos
-
special value returned by various string functions
-
maximum length of a string
s.find(u)
-
finds the subscript of the first occurence of u in s
-
u can be a string, a char, or a cstring
-
return string::npos if u does not occur in s.
s.find(u,start)
-
finds the subscript of the first occurence of u in s starting with
subscript start of u.
s.replace( start, numberOfCharsToRelace,
u)
-
at position start it replaces numberOfCharsToReplace characters with
u.
s.c_str()
-
returns the address of the Cstring piece of s where the characters
as stored.