String class

The std::string class is part of the C++ Standard Library and is used for representing and manipulating strings of characters.

It provides a rich set of member functions that allow you to perform various operations on strings, such as concatenation, searching, substring extraction, and more.

The std::string class is a dynamic, resizable, and efficient container for storing strings of characters.

Example of std::string:


Let’s demonstrate a simple example of using std::string and its important member functions.

std::string C++ Code:

#include <iostream>
#include <string>

int main() {
    // Creating and initializing strings
    std::string str1 = "Hello";
    std::string str2(" World!");

    // Concatenation
    std::string result = str1 + str2;

    // String length
    std::cout << "Length of the concatenated string: " << result.length() << std::endl;

    // Accessing characters
    std::cout << "First character: " << result[0] << std::endl;

    // Substring
    std::string substring = result.substr(6, 5); // starting at index 6, take 5 characters
    std::cout << "Substring: " << substring << std::endl;

    // Finding a substring
    size_t pos = result.find("World");
    if (pos != std::string::npos) {
        std::cout << "Found 'World' at position: " << pos << std::endl;
    } else {
        std::cout << "'World' not found." << std::endl;
    }

    // Erasing a portion of the string
    result.erase(5, 6); // starting at index 5, erase 6 characters

    // Inserting into the string
    result.insert(5, " Everyone");

    std::cout << "Modified string: " << result << std::endl;

    return 0;
}

Output:

Length of the concatenated string: 12
First character: H
Substring: World
Found 'World' at position: 5
Modified string: Hello Everyone!

Explanation of the Code:


In this example, we demonstrate the use of std::string and some of its important member functions:

Creating and Initializing Strings:

  • We create two strings str1 and str2 using different initialization methods.

Concatenation:

  • We concatenate str1 and str2 using the + operator to create a new string result.

String Length:

  • We use the length() function to get the length of the concatenated string result.

Accessing Characters:

  • We access the first character of the result using the indexing operator [].

Substring:

  • We extract a substring of result starting at index 6 and taking 5 characters using the substr() function.

Finding a Substring:

  • We search for the substring “World” in result using the find() function. If found, the position is printed; otherwise, a message is displayed.

Erasing and Inserting:

  • We erase a portion of the string result starting at index 5 and 6 characters long using the erase() function.
  • We insert the substring ” Everyone” at index 5 using the insert() function.

Important Member Functions of std::string:

  • length(), size(): Returns the length/size of the string.
  • operator[]: Accesses individual characters in the string.
  • substr(): Extracts a substring from the string.
  • find(), rfind(): Finds a substring in the string (from left-to-right or right-to-left).
  • erase(): Erases a portion of the string.
  • insert(): Inserts characters into the string at a specific position.
  • replace(): Replaces a portion of the string with a new string.
  • compare(): Compares two strings lexicographically.
  • empty(): Checks if the string is empty.
  • clear(): Clears the content of the string.
  • c_str(): Returns a C-style const char* pointer to the underlying character array.

These are some of the important member functions provided by the std::string class. There are more member functions available, making std::string a versatile and powerful tool for working with strings in C++.