0
0
C++programming~5 mins

String functions overview in C++

Choose your learning style9 modes available
Introduction

String functions help us work with text easily. They let us change, check, and find parts of words or sentences.

When you want to join two words or sentences.
When you need to find if a word contains a certain letter or part.
When you want to change letters from uppercase to lowercase or vice versa.
When you want to count how many letters or words are in a sentence.
When you want to cut out a part of a sentence.
Syntax
C++
std::string str = "Hello";

// Common string functions:
str.length();       // Get length of string
str.empty();        // Check if string is empty
str.append(" World"); // Add text at the end
str.substr(pos, len); // Get part of string
str.find("lo");    // Find position of substring
str.compare(other);  // Compare two strings
str.erase(pos, len); // Remove part of string
str.replace(pos, len, "new"); // Replace part of string
std::transform(str.begin(), str.end(), str.begin(), ::toupper); // Convert to uppercase

All string functions are called on a string object using dot . notation.

Positions (pos) start at 0, meaning the first letter is at position 0.

Examples
This gets the length of the word "Hello", which is 5.
C++
std::string greeting = "Hello";
int len = greeting.length();
This adds " World" to the end, making the string "Hello World".
C++
std::string text = "Hello";
text.append(" World");
This takes 3 letters starting from position 1, so part becomes "ell".
C++
std::string word = "Hello";
std::string part = word.substr(1, 3);
This finds where "lo" starts in "Hello", which is position 3.
C++
std::string word = "Hello";
int pos = word.find("lo");
Sample Program

This program shows common string functions: length, append, substring, find, replace, and converting to uppercase.

C++
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main() {
    std::string text = "Hello";
    std::cout << "Original text: " << text << "\n";

    // Length
    std::cout << "Length: " << text.length() << "\n";

    // Append
    text.append(" World");
    std::cout << "After append: " << text << "\n";

    // Substring
    std::string part = text.substr(6, 5);
    std::cout << "Substring (6,5): " << part << "\n";

    // Find
    size_t pos = text.find("World");
    std::cout << "Position of 'World': " << pos << "\n";

    // Replace
    text.replace(6, 5, "C++");
    std::cout << "After replace: " << text << "\n";

    // To uppercase
    std::transform(text.begin(), text.end(), text.begin(), ::toupper);
    std::cout << "Uppercase: " << text << "\n";

    return 0;
}
OutputSuccess
Important Notes

Remember string positions start at 0, so the first letter is position 0.

Functions like find return std::string::npos if the part is not found.

Use std::transform with care to change case; it needs <algorithm> and cctype headers.

Summary

String functions help us work with text easily.

Common tasks include finding length, adding text, cutting parts, and changing case.

Positions in strings start at zero, so count carefully.