The String Class

The string class is an extension that was added to C++ that deals with character strings as strings, not as arrays. Read this article to learn how to use classes to call methods and how to manipulate strings to access text data.

C++ String class

C and C++ do not have a native string data type.

Instead they rely on a null terminated char array, and are initialised in C like so:

char chArr[] = My new house ;
char *ptrArr = You should see my new house ;

C++ has an alternative method of handling strings that uses the string class, thus creating a string object.

Requires the #include <string> preprocessor directive, allowing string to be used like any other data type.

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

Declared / defined by simply using the string keyword:

  
string myString ;  //declaration

myString = Hip Priests and Kamerads ;  //definition

string yourString = This Nations Saving Grace ;  //initialisation

string cppString(“C++ string text goes in here”) ; //initialisation

The last example above sends a double quoted list of characters as the parameter to the string class, to create the string object containing the quoted characters.

Example using the .length() method of the string class:

#include <iostream>
#include <string>
using namespace std;
 
int main () {
 
    string myString ;
 
    myString = "The amazing Spiderman" ;
 
    for (int i = 0 ; i < myString.length() ; i++) {
 
        cout << "Array element ["<< i <<"] contains: " << myString[i] << endl ;
    }
    cout << myString << endl ;
 
    return 0;
}

Compile & Run:

Array element [0] contains: T
Array element [1] contains: h
Array element [2] contains: e
Array element [3] contains:
Array element [4] contains: a
Array element [5] contains: m
Array element [6] contains: a
Array element [7] contains: z
Array element [8] contains: i
Array element [9] contains: n
Array element [10] contains: g
Array element [11] contains:
Array element [12] contains: S
Array element [13] contains: p
Array element [14] contains: i
Array element [15] contains: d
Array element [16] contains: e
Array element [17] contains: r
Array element [18] contains: m
Array element [19] contains: a
Array element [20] contains: n
The amazing Spiderman

Example using the iterator, begin() and end() methods of the string class, and then dereferencing a pointer to access the individual characters within the string object:


C string class methods

#include <iostream>
#include <string>
using namespace std;
 
int main () {
 
    string yourString = "The Green Goblin" ;
 
    int i = 0 ;
 
    for (string::iterator it = yourString.begin() ; it < yourString.end() ; ++it, i++) {
 
        cout << "Array element ["<< i <<"] contains: " << *it << endl ;
    }
    cout << yourString << endl ;
 
    string cppString = "string" ;
 
    return 0;
}

Compile & Run:

Array element [0] contains: T
Array element [1] contains: h
Array element [2] contains: e
Array element [3] contains:
Array element [4] contains: G
Array element [5] contains: r
Array element [6] contains: e
Array element [7] contains: e
Array element [8] contains: n
Array element [9] contains:
Array element [10] contains: G
Array element [11] contains: o
Array element [12] contains: b
Array element [13] contains: l
Array element [14] contains: i
Array element [15] contains: n
The Green Goblin

Source: Derrick Robinson, http://web.archive.org/web/20160820094657/http://cpp.tech-academy.co.uk/c-string-class/
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.

Last modified: Monday, August 16, 2021, 10:52 AM