Commonly Used String Functions in C++

The std::string class contains functions to provide some common string operations. The below table contains some of the most commonly used functions in C++:

S. No.  

Category

Functions and Operators

Functionality

1.

String Length

length() or size()

It will return the length of the string.

2.

Accessing Characters

Indexing (using array[index])

To access individual characters using array indexing.

at()

Used to access a character at a specified index.

3.

Appending and Concatenating Strings

+ Operator

+ operator is used to concatenate two strings.

append()

The append() function adds one string to the end of another.

4.

String Comparison

== Operator

You can compare strings using the == operator.

compare()

The compare() function returns an integer value indicating the comparison result.

5.

Substrings

substr()

Use the substr() function to extract a substring from a string.

6.

Searching

find()

The find() function returns the position of the first occurrence of a substring.

7.

Modifying Strings

replace()

Use the replace() function to modify a part of the string.

insert()

The insert() function adds a substring at a specified position.

erase()

Use the erase() function to remove a part of the string.

8.

Conversion

c_str()

To obtain a C-style string from a std::string, you can use the c_str() function.

Note: The above functions only works for C++ Style strings (std::string objects) not for C Style strings (array of characters).

String Functions In C++

A string is referred to as an array of characters. In C++, a stream/sequence of characters is stored in a char array. C++ includes the std::string class that is used to represent strings. It is one of the most fundamental datatypes in C++ and it comes with a huge set of inbuilt functions. In this article, will look at the functions of string computations.

Similar Reads

What is std::string?

The std::string is a class in C++ since C++98. This class is the standard representation for a text string. It includes some typical string operations like find, replace, concatenate, compare etc. It is present in header file....

Commonly Used String Functions in C++

The std::string class contains functions to provide some common string operations. The below table contains some of the most commonly used functions in C++:...

1. String Length – length() or size()

We can find the length of the string (number of characters) using either length() or size() function of the std::string class....

2. Accessing Characters – at()

Generally, we can access the character of a string using the [] array subscript operator and indexing. But std::string also has a function named at() which can be used to access the characters of the string....

3. Concatenating Strings – append() or + Operator

We can concatenate string in C++ using two methods:...

4. String Comparison – compare() or == Operator

Just like the concatenation, we can do the string comparison using two methods:...

5. Searching –  find()

We can use the find() function of the std::string class to check whether a given character or a substring is present in the string or a part of string....

6. Generate Substring – substr()

We can use the substr() function to generate a part of the string as a new string object. It is a member function of the std::string class....

Modifying Strings

The following function allows us to modify the current string....

Convert std::string to C String – c_str)_

The c_str() function is a member function that is used to convert the C++ style string i.e. std::string objects to C style string i.e. array of characters....

Example of String Functions in C++

The below code demonstrate the use of the above specified string functions:...

Contact Us