References

Concatenation

Concatenation of different variable types

Numbers and strings don’t concat each other. Attempting to concat numbers and strings results in an error.

int integer = 4;
string random = "5";
cout << integer + random; // Errors

Using + operator between each string merges all the strings together to make a whole new string.

#include <iostream>
#include <string>
 
using namespace std;
 
int main() {
	string firstName = "John";
	string middleName = "Appleseed";
	string lastName = "Williams";
	
	string fullName = firstName + middleName + lastName;
	cout << "Full name: " << fullName;
	return 0;
}

Concatenation isn’t limited to just using variables. You could replace those variables with the string values, and it would still work the same.

cout << "Full name: " + "John" + "Appleseed" + "Williams";
// Output: Full name: JohnAppleseedWilliams

On the other hand, it’s nice to include spaces in between each string like this by adding in a spacer string (" "):

cout << "Full name: " + "John" + " " + "Appleseed" + " " + "Williams";
// Output: Full name: JohnAppleseedWilliams

Append

A similar operation to concatenation is string.append() Imagine you want to append another string on a string. You’d have to do something like this:

string welcome = "Willkommen,";
welcome.append(" world!");
cout << welcome;

Note that if you use append on a string, that string will replace the old string.

Notice how the first value of string is "Willkommen,", and after you appended the string, the new string value is Willkommen, world!.


Length

In order to retrieve the length of string, you use string.length() String length is the number of characters in a particular string.

string example = "1234567890"
cout << "The length of the string is " << example.length();

If you count the number of characters there are in the string, it’s like:

12345678910
1121231234123451234561234567123456781234567891234567890

An alternative method of getting the length of a string is string.size()


Accessing Characters

To retrieve a letter in the string, you can reference a specific index of the string. string is a sequence of characters so the first index of the sequence is 0.

Index01234
Characterhello
string myString = "hello";
cout << myString[0];
// Outputs h

If you want to get the last letter of the string, simply use string.length() and subtract 1 (if you recall correctly about the first index, which is 0)

string myString = "hello";
cout << myString[myString.length() - 1];
// Outputs o

at() function

A similar function of accessing characters is string.at() Using the previous example,

cout << myString.at(0); // Outputs h
cout << myString.at(myString.length() - 1); // Outputs o

Modifying characters

To modify a specific char in the string, you refer back to the index number of the string and assign it to a new character similar to reassigning a value to a char variable.

string myString = "Cat";
myString[0] = "K";
cout << myString;
// Outputs Kat

Finding a substring

A substring is a portion of the string. To search a substring, you use string.find()

#include <iostream>
#include <string>
 
using namespace std;
 
int main() {
	string text = "Coding is fun!";
	string keywords = "fun";
	
	size_t keywordsFoundAt = text.find(keywords);
	
	if (keywordsFoundAt != string::npos)
		cout << "Found the keywords at index " << keywordsFoundAt << endl;
	
	return 0;
}

In the example, the function finds the keywords of the text at index 11. If we had set keywords differently to anything or any string that isn’t found in the text, keywordsFoundAt returns string::npos, which is also the same as -1.


Extracting a substring

To extract a substring, you use string.substr(). The first argument of the string.substr() is the index number of the string (7) and the second argument is the length after the index (5).

#include <iostream>
#include <string>
 
using namespace std;
 
int main() {
	string text = "Hello World!";
	string sub = text.substr(7, 5);
	
	cout << "Substring: " << sub; // Substring: World
	return 0;
}

In the example, we specified 7 to start the extraction from index 7, which is the 8th character of the string, and plus the length of the extraction 5. The extraction length starts on the index you specified, not one after the index.


Converting characters to UPPERCASE

To convert characters into uppercase, use toupper(). Before using that function, you must include a library cctype.

#include <cctype>

toupper() and tolower() with strings

These two functions don’t accept string in the first argument. You must specify a character!!

Here’s an example:

#include <iostream>
#include <string>
#include <cctype>
 
using namespace std;
 
int main() {
	string what = "Hello World!";
	int i;
	for (i = 0; i < what.length(); i++) {
    	what[i] = toupper(what[i]);
    };
    
    cout << what; // Outputs HELLO WORLD!
    
    return 0;
}

Lowercase

An alternative method is tolower() similar to toupper() but changing all letters to lowercase.

#include <iostream>
#include <string>
#include <cctype>
 
using namespace std;
 
int main() {
	string what = "Hello World!";
	int i;
	for (i = 0; i < what.length(); i++) {
    	what[i] = tolower(what[i]);
    };
    
    cout << what; // Outputs hello world!
    
    return 0;
}

Converting string literal to char

string letter = "o";
char let = letter[0];