C++ String manipulation functions


String manipulation functions in C++ are used to perform various operations on strings, allowing developers to manipulate, analyze, and process string data easily. The C++ Standard Library provides a robust set of functions and methods for working with strings, particularly through the std::string class. Below is an overview of common string manipulation functions and methods available in C++.

1. Common std::string Member Functions

The std::string class offers various member functions to facilitate string manipulation. Here are some of the most frequently used functions:

a. Length and Size

  • length() / size(): Returns the number of characters in the string.
std::string str = "Hello, World!"; std::cout << "Length: " << str.length() << std::endl; // Output: 13

b. Accessing Characters

  • operator[]: Accesses a character at a specific index.
char firstChar = str[0]; // 'H'
  • at(index): Similar to operator[], but it performs bounds checking and throws an exception if the index is out of range.
char secondChar = str.at(1); // 'e'

c. Appending Strings

  • append(): Appends a string or character to the end of the current string.
str.append(" How are you?"); std::cout << str << std::endl; // Output: Hello, World! How are you?
  • operator+=: Concatenates another string or character to the current string.
str += " Have a nice day!";

d. Inserting Strings

  • insert(index, str): Inserts a string at the specified index.
str.insert(7, "Beautiful "); // Inserts "Beautiful " at index 7

e. Erasing Strings

  • erase(index, count): Removes characters from the string starting at the specified index for the given count.
str.erase(7, 10); // Removes "Beautiful "

f. Finding Substrings

  • find(substr): Returns the index of the first occurrence of the specified substring. Returns std::string::npos if not found.
size_t position = str.find("World"); if (position != std::string::npos) { std::cout << "Found 'World' at position: " << position << std::endl; // Output: 7 }

g. Replacing Substrings

  • replace(start, length, str): Replaces a portion of the string with another string.
str.replace(7, 5, "Everyone"); // Replaces "World" with "Everyone"

h. Substrings

  • substr(start, length): Returns a new string containing a substring from the original string, starting at the specified index and extending for the given length.
std::string sub = str.substr(0, 5); // Extracts "Hello"

i. Comparing Strings

  • compare(str): Compares the current string with another string and returns an integer value indicating the relationship.
if (str.compare("Hello, Everyone!") == 0) { std::cout << "Strings are equal." << std::endl; } else { std::cout << "Strings are not equal." << std::endl; }

j. Converting to C-style String

  • c_str(): Returns a pointer to a null-terminated character array representing the string.
const char* cStr = str.c_str(); // Convert to C-style string

2. Example of String Manipulation

Here’s a comprehensive example demonstrating various string manipulation functions:

#include <iostream> #include <string> int main() { std::string str = "Hello, World!"; // Print the original string std::cout << "Original String: " << str << std::endl; // Length of the string std::cout << "Length: " << str.length() << std::endl; // Accessing characters std::cout << "First character: " << str[0] << std::endl; // Appending str.append(" How are you?"); std::cout << "After append: " << str << std::endl; // Inserting str.insert(7, "Beautiful "); std::cout << "After insert: " << str << std::endl; // Erasing str.erase(7, 10); // Removes "Beautiful " std::cout << "After erase: " << str << std::endl; // Finding a substring size_t pos = str.find("World"); if (pos != std::string::npos) { std::cout << "'World' found at position: " << pos << std::endl; } // Replacing a substring str.replace(7, 5, "Everyone"); std::cout << "After replace: " << str << std::endl; // Substring extraction std::string sub = str.substr(0, 5); std::cout << "Substring: " << sub << std::endl; // Output: Hello // Comparing strings if (str.compare("Hello, Everyone!") == 0) { std::cout << "Strings are equal." << std::endl; } else { std::cout << "Strings are not equal." << std::endl; } // Convert to C-style string const char* cStr = str.c_str(); std::cout << "C-style string: " << cStr << std::endl; return 0; }