100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

String Functions in C++

Explore the most commonly used std::string member functions — length, substr, find, append, at, empty, and c_str — with practical examples.

StringsBeginner7 min readJul 7, 2026
Analogies

1. Intro

The std::string class comes with many built-in member functions that let you inspect and modify text without writing manual loops. These functions cover common tasks such as measuring length, extracting substrings, searching for characters or words, appending text, and safely accessing individual characters. Knowing the core set of string functions is essential for writing clean, exam-ready C++ code.

🏏

Cricket analogy: std::string member functions are like a scorer's toolkit: instead of manually counting every letter of a player's name, .length() and .find() handle common tasks like measuring and searching without writing loops.

2. Syntax

cpp
#include <string>
using namespace std;

string s = "Hello World";

s.length();          // or s.size() -> number of characters
s.substr(0, 5);      // extract "Hello"
s.find("World");     // index where "World" starts, or string::npos
s.append("!");        // adds text to the end
s.at(1);              // safe access to character at index 1
s.empty();             // true if length is 0
s.c_str();             // C-style const char* version of the string

3. Explanation

.length() and .size() are equivalent and return the number of characters in the string. .substr(pos, len) returns a new string starting at index pos with up to len characters. .find(target) searches for target and returns its starting index, or the special constant string::npos if not found. .append(text) adds characters to the end of the string, similar to +=. .at(index) returns the character at a given position and throws an exception if the index is out of range, unlike operator[] which does not check bounds. .empty() is a quick way to test whether a string has zero length. .c_str() returns a null-terminated const char* view of the string, useful when interacting with C-style APIs.

🏏

Cricket analogy: "Virat Kohli".length() returns 11, .substr(6, 5) pulls out "Kohli", .find("Kohli") returns its starting index or string::npos if not found, .append(" (c)") marks him as captain, .at(0) safely gets 'V' with bounds checking unlike [0], .empty() checks for a blank scoresheet entry, and .c_str() converts the name for a legacy C-style API.

.at() performs bounds checking and throws std::out_of_range for an invalid index, while [] does not check bounds and causes undefined behavior on an invalid index. Prefer .at() when safety matters more than raw speed.

4. Example

cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
    string s = "Hello World";

    cout << "Length: " << s.length() << endl;
    cout << "Substring: " << s.substr(6, 5) << endl;
    cout << "Find 'World': " << s.find("World") << endl;
    cout << "Is empty: " << (s.empty() ? "yes" : "no") << endl;
    return 0;
}

5. Output

text
Length: 11
Substring: World
Find 'World': 6
Is empty: no

6. Key Takeaways

  • .length()/.size() return character count; .substr() extracts part of a string.
  • .find() returns the starting index of a match, or string::npos when nothing is found.
  • .at() performs bounds checking and throws on invalid index, unlike [].
  • .c_str() converts a std::string to a null-terminated C-style string for interoperability.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#StringFunctionsInC#String#Functions#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep