String Methods

Requires the #include <string> preprocessor directive.

 

Part of the std namespace, and therefore also requires the using namespace std ; preprocessor directive.

 

Here are some of the more common string methods:

 

+, += Joins / Concatenates strings
<< Output Stream
==, !=, < <=, >, >= Compare strings
>> Input Stream
myString.append() Append string/characters
myString.assign("string-goes-here") Same as: myString = "string-goes-here" ;
myString.at(pos) Returns character at pos
myString.begin() Returns iterator positioned at first character of string. Start pointer
myString.c_str() Returns C string pointer. Null terminated
myString.capacity() Returns size of allocated storage for string
myString.compare(string2) Returns 0 if strings compare equal
constructors string strOne() = "string goes here" ;

string strTwo("string goes here") ;

string strThree ; strThree = "Anorak" ;

string strFour (strOne) ;

char *testStr = "testing" ; string strFive(testStr) ;

string strSix(testStr, 4) ; //first 4 characters of testStr

string strSeven(strOne, pos, len) ; //copy strOne into StrSeven at pos, for len

string strEight(len, 'x') ; //len number of 'x' characters

myString.copy(ptrArr, len, pos) Copies substring of len from myString from pos, into array at ptrArr

Doesn not append a NULL to ptrArr, needs to be carried out separately

myString.data() Returns C string pointer. NOT NULL terminated
myString.empty() Returns 1 if empty string, or 0 if not empty string
myString.end() Returns iterator positioned at last character of string. End pointer
myString.erase() Erases the string. No parameters required
myString.find(string2) Returns position of FIRST occurrence of string2
myString.find_first_not_of("char/string") Returns position of FIRST char/string NOT defined in the "char/string" parameter list
myString.find_first_of("char/string") Returns position of FIRST char/string defined in the "char/string" parameter list
myString.find_last_not_of("char/string") Returns position of LAST char/string NOT defined in the "char/string" parameter list
myString.find_last_of("char/string") Returns position of LAST char/string defined in the "char/string" parameter list
getline(input_stream, string, delim) Extracts chars from input stream and stores in string until .max_size(), EOF or delim
myString.insert(pos, string) Insert a copy of string into myString at position pos
myString.length() Returns int length of string. No parameters required
myString.max_size() Returns integer defining maximum size of string possible
myString.rbegin() reverse begin
myString.rend() reverse end
myString.rfind(string2) Returns position of LAST occurrence of string2
myString.replace(pos, len, string2) Replace len characters in myString at pos, with string2
myString.resize(len, 'char') Resizes myString to len. Truncates if less. Expands if smaller. If 'character' is missing, filled with blanks
myString.rfind("char/string") Returns position of LAST char/string defined in the "char/string" parameter list
myString.size() Returns int size of string. Same as above. No parameters required
myString.substr(pos,len) Returns substring within myString from pos, for len characters
myString.swap(string2) Swaps string2 with myString

 

Leave a Reply