Function
Description
Syntax
size()
Returns the size of the string in bytes.
string_name.size()
length()
Returns the length/ number of characters in the string, including spaces. (same as size()).
string_name.length()
push_back()
Appends a character to the end of the string.
string_name.push_back('char')
pop_back()
Removes the last character of the string.
string_name.pop_back()
resize()
Resizes the string to the specified number of characters. If the new size is larger, it adds default characters ('\0').
string_name.resize(new_size)
swap()
Swaps the contents of two strings.
string_name1.swap(string_name2)
find()
Searches for/ finds the first occurrence of a substring and returns its index. If not found, it returns std::string::npos.
string_name.find("substring")
erase()
Removes/ erases a part of the string, starting from the given position.
string_name.erase(pos, len)
compare()
Compares two strings lexicographically.
string_name.compare("another_string")
substr()
Extracts a substring from the string. It returns a substring starting from a given position and of a specified length.
string_name.substr(pos, len)
at()
Accesses the character at a specified position.
string_name.at(pos)
copy()
Copies characters from a string into a char array.
string_name.copy(char_array, len, pos)
capacity()
Returns the total capacity of the string, i.e., how many characters it can hold without reallocating memory.
string_name.capacity()
replace()
Replaces part of the string with another string.
str.replace(pos, len, "new_str")
empty()
Checks if the string is empty.
str.empty()
append()
Adds another string to the end of the current string.
str.append("another_str")
shrink_to_fit()
Reduces the capacity of the string to fit its size, releasing unused memory.
str.shrink_to_fit()
data()
Returns a pointer to the underlying character array.
str.data()
c_str()
Returns a C-style string (null-terminated character array) equivalent to the string object.
str.c_str()
begin()
Returns an iterator to the first character of the string. Useful for iteration through the string.
string_name.begin()
end()
Returns an iterator to the end of the string. (Marks the end of iteration.)
string_name.end()
rbegin()
Returns a reverse iterator to the end of the string. Starts reverse iteration from the last character.
string_name.rbegin()
rend()
Returns a reverse iterator to the beginning of the string.
string_name.rend()
To embed this project on your website, copy the following code and paste it into your website's HTML: