0% found this document useful (0 votes)
79 views2 pages

C++ String Functions CheatSheet

Uploaded by

Shivam Denge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views2 pages

C++ String Functions CheatSheet

Uploaded by

Shivam Denge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C++ String Functions Cheat Sheet

Creation & Basic Info

string s = "abc"; // Creates a string


s.length() / s.size(); // Returns length (e.g., 3)
s.empty(); // Returns true if string is empty

Accessing Characters

s.at(i); // Char at index with bounds check


s[i]; // Access via subscript
s.front(); // First character
s.back(); // Last character

Modifying Strings

s.append("xyz"); // Adds to end: "abc" + "xyz" = "abcxyz"


s += "123"; // Append using operator: "abc123"
s.insert(pos, "abc"); // Insert at position
s.erase(pos, len); // Delete len chars from pos
s.replace(pos, len, "xyz"); // Replace substring
s.clear(); // Erases the string
s.swap(s2); // Swaps with another string

Searching & Comparing

s.find("abc"); // First occurrence


s.rfind("abc"); // Last occurrence
s.compare(s2); // Lexicographic compare (0=equal)

Substring & Conversion

s.substr(start, len); // Get substring


s.c_str(); // Convert to C-style string (const char*)

Traversing a String

string s = "hello";
for (char ch : s) {
cout << ch << " "; // h e l l o
}
C++ String Functions Cheat Sheet

Pro Tip

String operations in C++ STL can be O(n) in worst case.


Avoid unnecessary copies, prefer passing by reference when possible.

You might also like