πŸ“˜Strings in CPP 2023

n C++, there are several string methods provided by the Standard Template Library (STL) that allow you to manipulate strings efficiently.

Let's go through each of them with example code:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";

    // 1. length(): Returns the length of the string.
    std::cout << "Length of the string: " << str.length() << std::endl;

    // 2. size(): Same as length(), returns the length of the string.
    std::cout << "Size of the string: " << str.size() << std::endl;

    // 3. empty(): Checks if the string is empty.
    if (str.empty()) {
        std::cout << "String is empty." << std::endl;
    } else {
        std::cout << "String is not empty." << std::endl;
    }

    // 4. clear(): Clears the contents of the string.
    str.clear();
    std::cout << "After clearing the string: " << str << std::endl;

    // 5. push_back(char): Appends a character to the end of the string.
    str.push_back('H');
    str.push_back('i');
    std::cout << "After appending characters: " << str << std::endl;

    // 6. pop_back(): Removes the last character from the string.
    str.pop_back();
    std::cout << "After popping the last character: " << str << std::endl;

    // 7. insert(pos, string): Inserts a string into the original string at the specified position.
    str.insert(0, "Hello, ");
    std::cout << "After inserting a substring: " << str << std::endl;

    // 8. erase(pos, len): Erases a part of the string starting from the specified position.
    str.erase(0, 7);
    std::cout << "After erasing a part of the string: " << str << std::endl;

    // 9. replace(pos, len, new_string): Replaces a part of the string with a new string.
    str.replace(0, 5, "Hi");
    std::cout << "After replacing a substring: " << str << std::endl;

    // 10. substr(pos, len): Returns a substring from the original string starting from the specified position.
    std::string sub = str.substr(0, 2);
    std::cout << "Substring: " << sub << std::endl;

    // 11. find(string): Searches for the first occurrence of the specified string within the original string.
    size_t found = str.find("Hi");
    if (found != std::string::npos) {
        std::cout << "Found at index: " << found << std::endl;
    } else {
        std::cout << "Not found." << std::endl;
    }

    // 12. rfind(string): Searches for the last occurrence of the specified string within the original string.
    size_t rfound = str.rfind("Hi");
    if (rfound != std::string::npos) {
        std::cout << "Last occurrence found at index: " << rfound << std::endl;
    } else {
        std::cout << "Not found." << std::endl;
    }

    return 0;
}

The above code demonstrates various string methods like length(), size(), empty(), clear(), push_back(), pop_back(), insert(), erase(), replace(), substr(), find(), and rfind() along with their usage. The output of the code will be:

Remember that there are more string methods available in C++. Feel free to explore the C++ documentation for the <string> header to learn about other useful methods.

Here are explanations and example code for the remaining string methods in C++:

Output of the code:

These are the explanations and examples for the remaining string methods in C++. Each method provides a specific functionality to manipulate and inspect strings, making string handling in C++ more flexible and efficient. Ensure to include these explanations in your documentation to provide a comprehensive understanding of the capabilities of C++ strings.

Last updated

Was this helpful?