C++ String

In C++, strings are objects of the std::string class, which represents a sequence of characters. The std::string class provides various functions to perform operations on strings, such as concatenation, comparison, conversion, and more. Here’s a summary of some of the most commonly used string functions and their purposes:

Example:

<#include <iostream>>
<#include <string>>
using namespace std;

int main() {
    // Create a string object
    string str1 = "Hello";
    string str2 = "World";

    // Concatenate strings
    string str3 = str1 + " " + str2;
    cout << "Concatenated String: " << str3 << std::endl;

    // Find the length of the string
    cout << "Length of str3: " << str3.length() << std::endl;

    // Access a character in the string
    cout << "Character at index 1: " << str3.at(1) << std::endl;

    // Replace part of the string
    str3.replace(6, 5, "C++");
    cout << "Replaced String: " << str3 << std::endl;

    return 0;
}

Output:

Concatenated String: Hello World
Length of str3: 11
Character at index 1: e
Replaced String: Hello C++

C++ String Functions

Function Description
int compare(const string& str) Compares two string objects. Returns 0 if equal, a negative value if less, and a positive value if greater.
int length() Returns the length of the string.
void swap(string& str) Swaps the values of two string objects.
string substr(int pos, int n) Creates a new string object consisting of n characters starting from position pos.
int size() Returns the length of the string in terms of bytes.
void resize(int n) Resizes the length of the string to n characters.
string& replace(int pos, int len, const string& str) Replaces a portion of the string starting at position pos and spanning len characters with another string.
string& append(const string& str) Adds new characters at the end of the string object.
char& at(int pos) Accesses the character at the specified position pos.
int find(const string& str, int pos = 0) Finds the first occurrence of the specified string starting from position pos.
int find_first_of(const string& str, int pos = 0) Finds the first occurrence of any character in the specified string starting from position pos.
int find_first_not_of(const string& str, int pos = 0) Searches for the first character that does not match any in the specified string starting from pos.
int find_last_of(const string& str, int pos = string::npos) Searches for the last occurrence of any character in the specified string.
int find_last_not_of(const string& str, int pos = string::npos) Searches for the last character that does not match any in the specified string.
string& insert(int pos, const string& str) Inserts a string at the specified position pos.
int max_size() Returns the maximum length of the string that can be held.
void push_back(char ch) Adds a new character ch at the end of the string.
void pop_back() Removes the last character of the string.
string& assign(const string& str) Assigns a new value to the string.
int copy(char* s, int len, int pos = 0) Copies the substring of the specified length starting at position pos into the provided array.
char& back() Returns a reference to the last character of the string.
iterator begin() Returns an iterator pointing to the first character of the string.
int capacity() Returns the allocated space for the string.
const_iterator cbegin() const Returns a constant iterator pointing to the first character of the string.
const_iterator cend() const Returns a constant iterator pointing to the end (past the last character) of the string.
void clear() Removes all characters from the string, making it empty.
const_reverse_iterator crbegin() const Returns a constant reverse iterator pointing to the last character of the string.
const char* data() const Returns a pointer to an array that contains the same sequence of characters as the string.
bool empty() const Checks whether the string is empty.
string& erase(int pos = 0, int len = npos) Removes characters from the string as specified.
char& front() Returns a reference to the first character of the string.
string& operator+=(const string& str) Appends the string str at the end of the current string.
string& operator=(const string& str) Assigns a new value to the string.
char operator[](int pos) Retrieves the character at the specified position pos.
int rfind(const string& str, int pos = string::npos) Searches for the last occurrence of the specified string starting from pos.
iterator end() Returns an iterator pointing to the end (past the last character) of the string.
reverse_iterator rend() Returns a reverse iterator pointing to the position before the first character of the string.
void shrink_to_fit() Reduces the capacity of the string to fit its size.
const char* c_str() const Returns a pointer to a null-terminated array containing the characters of the string.
const_reverse_iterator crend() const Returns a constant reverse iterator pointing to the position before the first character.
reverse_iterator rbegin() Returns a reverse iterator pointing to the last character of the string.
void reserve(size_t len) Requests that the string capacity be at least enough to contain len characters.
allocator_type get_allocator() const Returns the allocator object associated with the string.