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 | It will return the length of the string. | |
2. |
Accessing Characters |
Indexing (using array[index]) |
To access individual characters using array indexing. |
Used to access a character at a specified index. | |||
3. |
Appending and Concatenating Strings |
+ Operator |
+ operator is used to concatenate two strings. |
The append() function adds one string to the end of another. | |||
4. |
String Comparison |
== Operator |
You can compare strings using the == operator. |
The compare() function returns an integer value indicating the comparison result. | |||
5. |
Substrings | Use the substr() function to extract a substring from a string. | |
6. |
Searching | The find() function returns the position of the first occurrence of a substring. | |
7. |
Modifying Strings | Use the replace() function to modify a part of the string. | |
The insert() function adds a substring at a specified position. | |||
Use the erase() function to remove a part of the string. | |||
8. |
Conversion | 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.
Contact Us