C++ String
In C++, String is an object of class type std::string
. This type of object represents a sequence or string of characters, hence called a string.
The following is an example for a string.
"Hello World"
This string contains 11 characters. In C++, the string constant is enclosed in double quotes.
string
keyword is used to specify the datatype of a variable.
string s = "Hello World";
In this tutorial, we will go through most used C++ String Operations with examples.
C++ String Tutorials
The following are tutorials based on string operations, and are categorized based on the type of operations done at string level, or character level.
Basics
- C++ Create Empty String C++ Tutorial to create an empty string using double quotes.
- C++ String Length C++ Tutorial to get the number of characters in the given string.
- C++ String Comparison C++ Tutorial to compare two given strings, if they are equal, or if one is greater or lesser than the other.
Find / Get / Access
- C++ Substring C++ Tutorial to find substring, defined by a starting index and ending index, in the given string.
- C++ Get character from String at Specific Index C++ Tutorial to get character at specific index from given string.
- C++ Get first character in string C++ Tutorial to get the first character in string using string::at() function or string[index].
- C++ Get last character in string C++ Tutorial to get the last character in string using string::at() function or string[index].
- C++ Find index of substring in string C++ Tutorial to find the index or position of the first occurrence of a specific substring in the given string.
- C++ Iterate over characters of string C++ Tutorial to iterate over the characters of a string using Foreach loop statement.
- C++ Print unique characters in a string C++ Tutorial to find and print the unique characters in a string to console output.
Checks
- C++ String Equals C++ Tutorial to check if two given strings are equal.
Manipulations
The following tutorials cover use cases where part or whole of the string value is manipulated.
- C++ Append String C++ Tutorial to append a string to another.
- C++ Concatenate Strings C++ Tutorial to concatenate or join two given strings.
- C++ String Reverse C++ Tutorial to reverse a given string.
- C++ String Replace C++ Tutorial to replace the substring in a string with another.
- C++ Swap Strings C++ Tutorial to swap string values in two variables.
- C++ Convert String to Uppercase C++ Tutorial to convert the given string into uppercase.
- C++ Convert String to Lowercase C++ Tutorial to convert the given string into lowercase.
Character Level Manipulations
- C++ Append character at end of string C++ Tutorial to append a given character to end of the string using Addition Assignment Operator.
- C++ Append character to beginning of string C++ Tutorial to append a given character to beginning of the string using Addition Operator.
- C++ Insert Character in String at Specific Index C++ Tutorial to insert a given character at specific index in string.
- C++ Remove Last Character in String C++ Tutorial to remove the last character from the string.
- C++ Replace specific character with another in a string C++ Tutorial to replace all the occurrences of a specific character with another character in a string using
replace()
method ofalgorithm
library.
Conversions
The following tutorials cover use cases like converting given string into other datatypes, or vice versa.
- C++ Convert string to integer C++ Tutorial to convert given string literal into an integer value.
- C++ Convert integer to string C++ Tutorial to convert given integer value into a string value using
std::to_string()
method. - C++ Convert string to char array C++ Tutorial to convert given string into an array of characters.
- C++ Convert char array to string C++ Tutorial to convert given array of characters into a string.
Conclusion
In this C++ Tutorial, we learned how to do some of the String Operations with the help of example programs and detailed tutorials.