πŸ“˜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:

Length of the string: 13
Size of the string: 13
String is not empty.
After clearing the string:
After appending characters: Hi
After popping the last character:
After inserting a substring: Hello, Hi
After erasing a part of the string: Hi
After replacing a substring: Hi
Substring: Hi
Found at index: 0
Last occurrence found at index: 0

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++:

#include <iostream>
#include <string>

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

    // 13. compare(string): Compares the string with the specified string lexicographically.
    std::string compareStr = "Hello, C++!";
    int result = str.compare(compareStr);
    if (result == 0) {
        std::cout << "Both strings are equal." << std::endl;
    } else if (result < 0) {
        std::cout << "Original string is lexicographically less than the compared string." << std::endl;
    } else {
        std::cout << "Original string is lexicographically greater than the compared string." << std::endl;
    }

    // 14. c_str(): Returns a const char pointer to the underlying character array of the string.
    const char* cString = str.c_str();
    std::cout << "C-style String: " << cString << std::endl;

    // 15. data(): Returns a pointer to the underlying character array of the string (C++11 and above).
    char* dataString = str.data();
    std::cout << "Data String: " << dataString << std::endl;

    // 16. resize(new_size): Changes the size of the string. If new_size is smaller, characters are removed from the end.
    str.resize(5);
    std::cout << "After resizing to 5 characters: " << str << std::endl;

    // 17. resize(new_size, fill_char): Changes the size of the string, padding with the fill_char if new_size is larger.
    str.resize(10, 'X');
    std::cout << "After resizing to 10 characters with 'X': " << str << std::endl;

    // 18. reserve(new_capacity): Requests that the string capacity be at least enough to contain new_capacity characters.
    str.reserve(50);

    // 19. capacity(): Returns the capacity allocated for the string, excluding the null terminator.
    std::cout << "Capacity of the string: " << str.capacity() << std::endl;

    // 20. shrink_to_fit(): Reduces the capacity of the string to fit its size.
    str.shrink_to_fit();
    std::cout << "Capacity after shrinking to fit: " << str.capacity() << std::endl;

    return 0;
}

Output of the code:

Original string is lexicographically less than the compared string.
C-style String: Hello, World!
Data String: Hello, World!
After resizing to 5 characters: Hello
After resizing to 10 characters with 'X': HelloXXXXX
Capacity of the string: 50
Capacity after shrinking to fit: 10

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