C++ Strings – A Reference

 


#include <string>                      

using namespace std;

 

OR

 

#include <string>            

using std::string;

 

 


string s1 = “hello”;

string s2 = s1;                         // s2 is “hello”

s1 = s1 + “ there”;                   // s1 is “hello there”

 

if (s1 == s2)            // are they equal?

cin >> s1;                // Read chars from keyboard into s1 until white space is encountered.

getline(cin, s1);       // Read chars from keyboard into s1 until \n is encountered.





Prototype                                 Example                                  Description

bool <-- empty()

string a = “test”;

if (a.empty());  // false

Returns true if nothing in string, false otherwise.

int <-- length()

int len = s1.length();  // 5

Returns number of chars in string.  Same as size().

char <-- at(int pos)

string a = “hello”;

char c = a.at(0);  // same as c = a[0]

a.at(0) = ‘H’;   // a is “Hello”

Returns char at position pos.  Same functionality as using [pos] notation.

char * <-- c_str()

string a = “hello”;

char *p = a.c_str();   // “hello”

Returns char array representing string with null terminator at the end.

int <-- compare(string s)

string a = “test”;

if (a.compare(“test”) == 0) // same

Compares string s, returns int.  Just like < > ==.  Returns 0 if object and s are equal, < 0 if object is before s, and > 0 if after s.

int <-- compare(int start,

  int numChars, string s)

string a = “compare test”;

if (a.compare(2, 3, “yes test”) == 0)

  // compare “mpa“ to “yes“

Compares numChars chars starting at slot start to string s.

Note: g++ compiler requires string to be first arg, not last.

erase(int pos)

string a = “erase”;

a.erase(2);  // Leaves “er”

Erases all chars starting at position pos.

append(string s, int numChars)

string a = “again”;

a.append(“state”, 2);   // “against”

Appends numChars chars from s to string.

resize(int size)

string a = “to”;

a.resize(4);

a[2] = “b”;  a[3] = “e”;   // a = “tobe”

Resizes string so it can hold additional characters.

string <-- substr(int start,

  int numChars)

string a = “a nice play”;

string b = a.substr(2, 4);   // “nice”

Return substring starting at slot start with numChars chars.

int <-- find(string s)

int <-- rfind(string s)

string a = “this is a test”;

int pos = a.find(“is”);  // returns 2

pos = a.rfind(“is”);  // returns 5

Returns position of first occurrence of s, string::npos if string is not found.  Reverse find starts searching at back of string.

int <-- find_first_of(string s)

int <-- find_last_of(string s)

string a = “this is a test”;

pos = a.find_first_of(“iou”);  // 2

pos = a.find_last_of(“iou”);  // 5

Returns first (or last) occurrence of any char in s.

int <-- find_first_not_of(string s)

int <-- find_last_not_of(string s)

string a = “this is a test”;

pos = a.find_first_not_of(“this”);  // 4

pos = a.find_last_not_of(“this”);  // 11

Returns first (or last) occurrence of any char not in s.

replace(int start, int numChars,

  string s)

string a = “hello there”;

s1.replace(0, 1, “play c”); 

  // “play cello there”

Replaces numChars chars starting at position start with s.

insert(int start, string s,

  int sStart, int numChars)

a = “this is a test”;

a.insert(8, “not “, 0, string::npos);

  // “this is not a test”

Inserts string s at position start.  sStart is starting position in s, and numChars is number of chars in s.

swap(string s)

string a = “one”, b = “two”;

a.swap(b);  // a = “two”, b = “one”

Swaps string s with string object.

 

 

 


// Prints all items in a string that are separated by a common delimiter.

 

void parse(string parseString, string delimiter)

{

  string value;

 

  int startPos = 0, pos = parseString.find(delimiter);

  while (pos != string::npos)

  {

    value = parseString.substr(startPos, pos - startPos);

    cout << value << endl;

    startPos = pos + delimiter.length();

    pos = parseString.find(delimiter, startPos);

  }

 

  value = parseString.substr(startPos, parseString.length() - startPos);

  cout << value << endl;

}


// Example call:

 

parse("this::is::a::test", "::");