String Handling Functions
We can perform many operations on strings such as concatenation, comparision, getting substring, inserting, erase, finding index value etc using available functions called String handling Functions. The string can be declared by using the keyword string. Let us discuss some available string handling functions with its definition.
Function Name | Description |
---|---|
length() | It is used to calculate total number of characters. |
empty() | It is used to check whether string is having any value or not. |
clear() | It is used to clear all the values in a string. |
append() | It is used to add one string to another string in the end. |
at() | It is used to find character using index number. |
substr() | Substring starts at a specified character position and continues to the end of the string. |
insert() | It is used to check whether the beginning of this string instance matches the specified string. |
find() | It is used to find index number using character. |
erase() | It is used to erase characters. |
CODE
EG 1 . Creating a string and working on some string handling functions.
#include <iostream>
using namespace std;
int main() {
    string name = "CodersCapsule";
    cout << name.length() << endl; //Output : 13
    cout << name.empty() << endl; //Output : 0(which means false)
    cout << name.append(".com") << endl; //Output : CodersCapsule.com
    cout << name.at(2) << endl; //Output : d
    cout << name.substr(6) << endl; //Output : Capsule
    cout << name.find('r') << endl; //Output : 4
    cout << name.erase(6) << endl; //Output : Coders
}
(Click Here, For more exercises)