C++ String Functions

About This Index
This page is the C++ String Functions index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs.
Learn what a string is on /cpp/string. Use this page when you need a specific function such as find(), append(), or stoi().
Most everyday C++ text uses std::string from <string>. The <cstring> helpers remain for C APIs and interviews. The 86 guides below cover both families.
How to Call a String Function
Members use a dot on a std::string. Free helpers and C APIs are called on their own:
#include <iostream>
#include <string>
#include <cstring>
std::string s = "Hello";
s.append(" C++"); // member
std::size_t p = s.find("C++"); // member, or npos
int n = std::stoi("42"); // free helper
std::size_t len = std::strlen("hi"); // <cstring> C++ String Functions Table
All function guides in this C++ tutorial series (86 tutorials), grouped by purpose.
Capacity
7 functionsAsk how long a string is and manage buffer room.
| Function | Description | Tutorial |
|---|---|---|
empty() | Check whether a std::string holds no characters. | Try it |
size() | Return how many characters the string currently holds. | Try it |
length() | Same as size(): how many characters are in the string. | Try it |
max_size() | Return the theoretical maximum number of characters this string type can hold. | Try it |
capacity() | How many characters the current buffer can hold without reallocating. | Try it |
reserve() | Request that the string buffer hold at least n characters without reallocating. | Try it |
shrink_to_fit() | Non-binding request to reduce capacity toward size(). | Try it |
Element access
6 functionsRead characters and raw buffers. at() checks bounds; operator[] does not.
| Function | Description | Tutorial |
|---|---|---|
at() | Read or change a character by index; throws if out of range. | Try it |
front() | Reference to the first character. Undefined on an empty string. | Try it |
back() | Reference to the last character. Undefined on an empty string. | Try it |
data() | Pointer to the string’s contiguous character buffer. | Try it |
c_str() | const char* to a null-terminated C-string view of the text. | Try it |
copy() | Copy characters into a caller-provided char buffer (no added null). | Try it |
Modifiers
15 functionsChange the text: insert, erase, append, replace, resize, and swap.
| Function | Description | Tutorial |
|---|---|---|
clear() | Remove all characters; size becomes 0. | Try it |
insert() | Insert characters at a position or iterator. | Try it |
insert_range() | Insert all characters from a range at a position (C++23). | Try it |
erase() | Remove characters by index, count, or iterator range. | Try it |
push_back() | Append one character at the end. | Try it |
pop_back() | Remove the last character. Undefined on empty strings. | Try it |
append() | Add characters or another string at the end. | Try it |
append_range() | Append all characters from a range (C++23). | Try it |
assign() | Replace the entire contents with new characters. | Try it |
assign_range() | Replace contents with all characters from a range (C++23). | Try it |
replace() | Replace a slice of the string with new characters. | Try it |
replace_with_range() | Replace an iterator range with another range (C++23). | Try it |
resize() | Change the number of characters; pad or truncate. | Try it |
resize_and_overwrite() | Resize and let a callback write the new characters (C++23). | Try it |
swap() | Exchange contents with another string. | Try it |
Search
9 functionsFind text and test prefixes/suffixes. find family returns npos when missing.
| Function | Description | Tutorial |
|---|---|---|
find() | Search for a substring or character; returns index or npos. | Try it |
rfind() | Find the last occurrence of a substring or character. | Try it |
find_first_of() | Find the first character that matches any in a set. | Try it |
find_last_of() | Find the last character that matches any in a set. | Try it |
find_first_not_of() | Find the first character that is not in a given set. | Try it |
find_last_not_of() | Find the last character that is not in a given set. | Try it |
contains() | Return true if the string contains a substring or character (C++20). | Try it |
starts_with() | Test whether the string begins with a prefix (C++20). | Try it |
ends_with() | Test whether the string ends with a suffix (C++20). | Try it |
Operations
3 functionsCompare and slice strings.
Conversions
10 functionsParse numbers from text and build text from numbers.
| Function | Description | Tutorial |
|---|---|---|
stoi() | Convert a string to int. | Try it |
stol() | Convert a string to long. | Try it |
stoll() | Convert a string to long long. | Try it |
stoul() | Convert a string to unsigned long. | Try it |
stoull() | Convert a string to unsigned long long. | Try it |
stof() | Convert a string to float. | Try it |
stod() | Convert a string to double. | Try it |
stold() | Convert a string to long double. | Try it |
to_string() | Convert a number to std::string. | Try it |
to_wstring() | Convert a number to std::wstring. | Try it |
I/O
1 functionRead a whole line into a std::string.
Iterators
8 functionsWalk characters forward or backward. The c- versions are read-only.
| Function | Description | Tutorial |
|---|---|---|
begin() | Iterator to the first character (or end() if empty). | Try it |
end() | Iterator one past the last character—the loop stop mark. | Try it |
cbegin() | const_iterator to the first character (read-only). | Try it |
cend() | const_iterator one past the last character. | Try it |
rbegin() | Reverse iterator to the last character. | Try it |
rend() | Reverse iterator one before the first character—reverse stop mark. | Try it |
crbegin() | const reverse iterator to the last character. | Try it |
crend() | const reverse past-the-end iterator. | Try it |
Allocator
1 functionInspect the allocator that manages string memory.
| Function | Description | Tutorial |
|---|---|---|
get_allocator() | Return a copy of the allocator that manages the string’s memory. | Try it |
C string (<cstring>)
26 functionsClassic C string and memory helpers still seen in interviews and C APIs.
| Function | Description | Tutorial |
|---|---|---|
strlen() | Return the length of a null-terminated C-string. | Try it |
strcpy() | Copy a C-string into a destination buffer. | Try it |
strncpy() | Copy at most n characters into a destination buffer. | Try it |
strcat() | Append one C-string onto another. | Try it |
strncat() | Append at most n characters from a C-string. | Try it |
strcmp() | Compare two C-strings lexicographically. | Try it |
strncmp() | Compare at most n characters of two C-strings. | Try it |
strcoll() | Compare two C-strings using the current locale. | Try it |
strchr() | Find the first occurrence of a character in a C-string. | Try it |
strrchr() | Find the last occurrence of a character in a C-string. | Try it |
strpbrk() | Find the first character that matches any in a set. | Try it |
strspn() | Count leading characters that belong to an accept set. | Try it |
strcspn() | Count leading characters not in a reject set. | Try it |
strstr() | Find the first occurrence of a C-string inside another. | Try it |
strtok() | Split a C-string into tokens (mutates; not thread-safe). | Try it |
strerror() | Turn an errno code into a message string. | Try it |
strxfrm() | Transform a C-string for locale-sensitive comparison with strcmp. | Try it |
strdup() | Allocate a copy of a C-string with malloc (POSIX). | Try it |
strndup() | Allocate a copy of at most n characters of a C-string (POSIX). | Try it |
memcpy() | Copy bytes from source to destination (buffers must not overlap). | Try it |
memmove() | Copy bytes; safe when source and destination overlap. | Try it |
memset() | Fill a byte range with one value. | Try it |
memset_explicit() | Fill memory with intent that the store is not optimized away (C23). | Try it |
memchr() | Search for a byte in a memory block. | Try it |
memcmp() | Compare two byte blocks; return <0, 0, or >0. | Try it |
memccpy() | Copy bytes until a given byte is copied or n bytes are done. | Try it |
Frequently Asked Questions
Start with append()
Add text to the end of a std::string—the first edit most programs need.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
12 people found this page helpful
