A Review of the String Dataypte
- Strings are discussed in chapters 2 and three.
- Some string specifics
- A string is a collection of 0 or more characters.
-
#include <string>
- gcc does not seem to require this, but it is good to include it.
- It is part of the standard template library, but you may use strings and their member functions unless explicitly forbidden.
- If you don't use using namespace std; all the use of string needs to be proceeded by sdt::, for example std::string word
- More on this later.
- But you may see it as you look at other code.
- When a string variable is declared, it is set to the empty string or "".
- Strings have many member functions
- size() or length() gives the number of characters.
- + is string concatenation, used to combine strings.
- [i] or at(i) can be used to access individual characters
- The value for i above must be between 0 and the size of the string -1.
- Letters are numbered from 0.
- find will search the string for a substring.
- It will either return the position of the substring, a size_t
- or the constant string::npos
- rfind will search from the end.
- substr(start, len) will extract a substring from the string
- starting at position start, a size_t
- For len letters
- If len not supplied, it will go to the end of the string.
- If len is longer than the remaining characters, it is not a problem.
- There are two c libraries that provide additional character manipulation functions
- is* family
- man isascii
- These are old so they take an int and return an int
- But they really take a char and return a bool
- You might need to cast to eliminate compiler warnings.
- They are useful when you want to identify different characteristics of a character.
- to* family
- man toupper
- These take an int and return an int.
- But really take a char and return a char
- Again you may need to cast.
- toupper only works on characters, NOT ON ENTIRE STRINGS.
- look at stringDemo.cpp