C++ STL String Operations and Usage Guide
This guide covers the C++ Standard Template Library (STL) string type, which encapsulates common string operations for ease of use. Too use it, include the <string> header.
String Definition
Define a string variable similarly to other variables:
std::string text;
String Initialization
Initialize by direct assignment:
std::string text = "sample";
String Input and Output
Output
Use cout directly:
#include <iostream>
#include <string>
int main() {
std::string data = "output";
std::cout << data;
return 0;
}
Input
Use cin directly:
#include <iostream>
#include <string>
int main() {
std::string input;
std::cin >> input;
std::cout << input;
return 0;
}
String Access
Access characters using endices, similar to arrays:
#include <iostream>
#include <string>
int main() {
std::string str;
std::cin >> str;
std::cout << str[2];
return 0;
}
String Copy
Copy strings with direct assignment:
#include <iostream>
#include <string>
int main() {
std::string source, destination;
std::cin >> source;
destination = source;
std::cout << "Source: " << source << "\nDestination: " << destination;
return 0;
}
String Concatenation
Combine strings using the + operator or append() function.
Direct Addition
#include <iostream>
#include <string>
int main() {
std::string first, second, combined;
std::cin >> first >> second;
combined = first + second;
std::cout << "First: " << first << "\nSecond: " << second << "\nCombined: " << combined;
return 0;
}
append(const char* str, int n)
The append() function can take one or two parameters:
#include <iostream>
#include <string>
int main() {
std::string base = "prefix ";
std::cout << "Before: " << base << '\n';
base.append("suffix", 3); // Appends first 3 characters
std::cout << "After partial append: " << base << '\n';
base = "prefix ";
base.append("suffix"); // Appends entire string
std::cout << "After full append: " << base;
return 0;
}
String Comparison
Compare strings using relational operators (==, !=, <, <=, >, >=), based on ASCII values from the first character onward. Returns 1 for true, 0 for false.
#include <iostream>
#include <string>
int main() {
std::string str1 = "abc", str2 = "abc", str3 = "acb";
std::cout << "str1 == str2: " << (str1 == str2) << '\n';
std::cout << "str1 <= str3: " << (str1 <= str3) << '\n';
std::cout << "str1 >= str3: " << (str1 >= str3);
return 0;
}
String Length
Get length with length() or size(), which return the actual length without null terminators.
#include <iostream>
#include <string>
int main() {
std::string example = "length";
std::cout << "String: " << example << '\n';
std::cout << "Length: " << example.length() << '\n';
std::cout << "Size: " << example.size();
return 0;
}
String Search with find(string str)
Find the first occurrence of a substring using find(). Returns the index if found, otherwise std::string::npos (typically -1).
#include <iostream>
#include <string>
int main() {
std::string mainStr = "searchable", subStr = "ch";
std::cout << "Find 'ch' in 'searchable': " << mainStr.find(subStr) << '\n';
std::cout << "Find 'xy' in 'searchable': " << mainStr.find("xy");
return 0;
}
Substring Extraction with substr(int a, int b)
Extract substrings using substr(). Parameters: start index and optional length.
#include <iostream>
#include <string>
int main() {
std::string original = "extract";
std::cout << "Original: " << original << '\n';
std::cout << "Substring from index 2, length 3: " << original.substr(2, 3) << '\n';
std::cout << "Substring from index 4 to end: " << original.substr(4);
return 0;
}
Case Conversion
Convert to Lowercase with tolower(char a)
Convert individual characters; cast result to char.
#include <iostream>
#include <string>
#include <cctype>
int main() {
std::string upper = "UPPER";
std::cout << "Original: " << upper << '\n';
std::cout << "Lowercase: ";
for (char ch : upper) {
std::cout << static_cast<char>(std::tolower(ch));
}
return 0;
}
Convert to Uppercase with toupper(char a)
Convert individual characters; cast result to char.
#include <iostream>
#include <string>
#include <cctype>
int main() {
std::string lower = "lower";
std::cout << "Original: " << lower << '\n';
std::cout << "Uppercase: ";
for (char ch : lower) {
std::cout << static_cast<char>(std::toupper(ch));
}
return 0;
}
ASCII Notes
- Uppercase letters are 32 less than lowercase letters in ASCII.
- Character digits differ from integer digits by 48.
#include <iostream>
int main() {
std::cout << "A: " << static_cast<int>('A') << '\n';
std::cout << "Z: " << static_cast<int>('Z') << '\n';
std::cout << "a: " << static_cast<int>('a') << '\n';
std::cout << "z: " << static_cast<int>('z') << '\n';
std::cout << "Difference a - A: " << 'a' - 'A' << '\n';
std::cout << "0: " << static_cast<int>('0') << '\n';
std::cout << "'1' - 48: " << '1' - 48;
return 0;
}