C++ String Functions

Beginner
8 min read
Updated: Nov 2026
86 function guides
Function index

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().

std::string and C-strings together.

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:

C++
#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 functions

Ask how long a string is and manage buffer room.

FunctionDescriptionTutorial
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 functions

Read characters and raw buffers. at() checks bounds; operator[] does not.

FunctionDescriptionTutorial
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 functions

Change the text: insert, erase, append, replace, resize, and swap.

FunctionDescriptionTutorial
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

Operations

3 functions

Compare and slice strings.

FunctionDescriptionTutorial
compare()Lexicographically compare with another string; returns <0, 0, or >0.Try it
substr()Return a new string that is a slice of this string.Try it
subview()Return a string_view into a slice of this string (C++26).Try it

Conversions

10 functions

Parse numbers from text and build text from numbers.

FunctionDescriptionTutorial
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 function

Read a whole line into a std::string.

FunctionDescriptionTutorial
getline()Read a whole line from an input stream into a std::string.Try it

Iterators

8 functions

Walk characters forward or backward. The c- versions are read-only.

FunctionDescriptionTutorial
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 function

Inspect the allocator that manages string memory.

FunctionDescriptionTutorial
get_allocator()Return a copy of the allocator that manages the string’s memory.Try it

C string (<cstring>)

26 functions

Classic C string and memory helpers still seen in interviews and C APIs.

FunctionDescriptionTutorial
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

They include std::string members such as find(), append(), and substr(), free helpers such as std::stoi() and std::getline(), and C APIs from <cstring> such as strlen() and memcpy().
This series lists 86 function tutorials across capacity, access, modifiers, search, conversions, iterators, and C-string helpers.
Prefer std::string for new C++ code. Use <cstring> helpers for C APIs, fixed buffers, and interview questions that still test them.
contains, starts_with, and ends_with are C++20. append_range, assign_range, insert_range, replace_with_range, and resize_and_overwrite are C++23. subview is documented for C++26.
Read the C++ Strings hub first, then open find(), append(), substr(), and stoi() from the tables below.

Start with append()

Add text to the end of a std::string—the first edit most programs need.

append() tutorial →

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful