CommonLounge Archive

C++ String Methods

September 11, 2018

We already learnt about strings in C++ Data-types, Operators and Strings, but in this tutorial we are going to learn a little more about C++ strings and also see some helpful functions C++ provides for strings.

String literals

String literals are enclosed in either double quotes.

For example, "Hello World" is a string.

Escaping

To type in some special characters, you have to type in a backslash escape. For example, \" is a double quote, \\ is a backslash, \t is a tab and \n is a newline. For example, the following code:

cout << "Alice said \"How do you do?\"\nBob replied, \"Very well thank you!\"";

Output:

Alice said "How do you do?"
Bob replied, "Very well thank you!"

Raw strings

A raw string literal is prefixed by an R and having format R"(string)" passes all the chars through without special treatment of backslashes, so R"(x\nx)" evaluates to the length-4 string "x\nx".

string raw = R"(this\t\n and that)";  
cout << raw << endl;     // this\t\n and that

Multiline strings

A string literal can span multiple lines, but there must be a backslash \ at the end of each line to escape the newline. String literals having format R"(string)" can also span multiple lines.

string raw = R"(Beware the Jabberwock, my son!
The jaws that bite, the claws that catch!
Beware the Jubjub bird, and shun
The frumious Bandersnatch!"
)";
cout << raw << endl;

Converting to string

The to_string() function converts values to a string form so they can be combined with other strings.

double pi = 3.14;  
// string  text = "The value of pi is " + pi;          // NO, does not work
string text = "The value of pi is "  + to_string(pi);  // yes
cout << text << endl;

Accessing string characters

C++ uses zero-based indexing (like most other programming languages), so if string is "hello", str[1] is 'e'. If the index is out of bounds for the string, C++ raises an error.

Modifying strings

C++ strings are mutable which means they can be changed after they are created. Characters in a string can be accessed using the standard [ ] syntax.

String Methods

Here are some of the most common string methods. A method is like a function, but it runs “on” an object. If the variable s is a string, then the code s.length() runs the length() method on that string object and returns the result (this idea of a method running on an object is one of the basic ideas that make up Object Oriented Programming, OOP).

Here are some of the most common string methods:

String length

s.length() — returns length of the string s

string s = "Hello";
cout << s.length() << endl;    
// Output - 5

Get substring of the string

s.substr(a, b) — get sub-parts of a string.

Some examples:

string s = "Hello";
// chars starting at index 1 and extending up to index 3
cout << s.substr(1, 3) << endl; // "ell" 
// the second index defaults to the end of the string when omitted
cout << s.substr(1) << endl; // "ello"  
// omitting both the indices always gives us a copy of the whole thing
cout << s.substr() << endl; // "Hello" 
// an index that is too big is truncated down to the string length
cout << s.substr(1, 100) << endl; // "ello"

Concatenation

The + operator can concatenate two strings.

string s = "Hello"; 
cout << s + " there" << endl;
// Output - Hello there

You can also use s.append(other)

Example :

string s = "Hello";
s.append(" there");
cout << s << endl;
// Output - Hello there

Find substring in string

s.find(other) searches for the given other string (not a regular expression) within s, and returns the first index where it begins or -1 if not found

Example:

string a = "hello there";
cout << a.find("the") << endl;
// Output 6

Replace or remove part of the string

s.replace(n, l, str) — Replaces l characters from nth index with str string

Example:

string a = "hello there";
a.replace(1, 4, "ey");
cout << a << endl;
// Output - hey there

s.erase(n, l)— deletes b characters at index a

Example:

string a = "hello there";
a.erase(2, 7);
cout << a << endl;
// Output - here

Characters and ASCII

Now, we’ll learn about something different - ASCII.

In C++, a char type can store 256 different values. These include lowercase and uppercase letters (a-z and A-Z), digits (0-9) and many special characters (like $, #, etc). However, behind the scenes C++ stores a char as an integer. For example, try:

cout << (int)'c' << endl; // 99 
cout << (char)99 << endl; // c 

The ASCII table below lists the integer values corresponding the first 128 ASCII characters (there are 256 total).

First 128 values of ASCII table

Most of the time, this doesn’t matter. But it’s quite helpful sometimes. For example, notice that characters A to Z occupy numbers 65 to 90 and a to z occupy numbers 97 to 122. Let’s see how we can use this.

Converting lowercase to uppercase

Since 'a' is 97 and 'A' is 65. So, to convert from lowercase to uppercase, all we need to do is subtract 32. For example:

cout << (char)('a' - 32) << endl; // A
cout << (char)('f' - 32) << endl; // F
cout << (char)('D' + 32) << endl; // d
cout << (char)('K' + 32) << endl; // k

A couple of other things. Firstly, if 32 is too hard to remember, you can always just do c = c + ('A' - 'a'). Secondly, C++ also has toupper() and tolower() functions pre-defined.

To convert the entire string to uppercase, you’ll need a loop. For example:

string a = "hello world";
for (int i = 0; i < a.length(); i++) {
    a[i] = toupper(a[i]);
}
cout << a << endl;
// Output - HELLO WORLD

Comparisons / character is alphabet, digit, etc

Let’s say we want to check if a character is a lowercase alphabet. We can simply do

char c1 = 'e';
cout << ('a' <= c1 && c1 <= 'z') << endl;
// 1 (true)
char c2 = 'E';
cout << ('a' <= c2 && c2 <= 'z') << endl;
// 0 (false)

When doing a comparison, C++ converts chars to integers, and then performs the comparison. So for example, do you think '+' < '=' is true or false? (see ASCII table). Let’s confirm:

cout << ('+' < '=') << endl;
// 1 (true)

Again, C++ already has functions like isalpha() / isdigit() / isspace() which check if the character is in the character class.

Comparing two strings in C++

Now that you know how comparing two chars works in C++, let’s look at how comparing two strings works. The logic is simple, C++ first compares the first characters of both the strings, then the second, then third, and so on. As soon as it finds two unequal characters, it knows which character is smaller, and hence which string is smaller.

Here are some examples:

cout << boolalpha;
string abc = "abc";
// 'a' is smaller than 'd'
cout << (abc < "def") << endl;
// true 
// 'A' is smaller than 'a' (see ASCII table)
cout << (abc < "ABC") << endl;
// false
// empty string is the smallest string 
cout << ("" < abc) << endl;
// true
// prefixes are smaller 
cout << (abc < "abca") << endl;
// true
// 'a' is smaller than '{' (see ASCII table)
cout << (abc < "{}") << endl;
// true
// '0' is smaller than 'a' (see ASCII table)
cout << (abc < "012") << endl;
// false

Note: In the examples above, we did string abc = "abc"; in the beginning. This is because in C++, comparing two string literals directly is not allowed.

Summary

In this tutorial, you learnt about

  • string literals - including literals with special characters and multi-line strings
  • accessing string characters - characters of a string can be accessed using [index]
  • string methods - all sorts of useful things we can do with strings
  • ASCII values - C++ stores characters as integers behind the scenes
  • string comparison - what C++ does behind the scenes when comparing two strings

© 2016-2022. All rights reserved.