Strings and Methods of Strings:

String is a primitive data type which is used to store text or sequence of characters. Strings are always enclosed in double quotation marks. String is an array of characters. We will discuss arrays later on.

string name = "Joseph";

Header file of string:

#include <string>

string.cpp
#include<iostream>
#include<string>
using namespace std;

int main(){
    string name = "Joseph";
    cout<<name;
    return 0;
}

Concatenation:

Concatenation means joining of two strings and it is achieved by using addition "+" operator.

string.cpp
#include<iostream>
#include<string>
using namespace std;

int main(){
    //Method 1:
    string street = "Street no 14";
    string house = "House no 1";
    string address = street + house; 
    cout << address <<endl;

    //Method 2:
    string address ="Street 14 " + "House 1";
    cout<<address <<endl;

    //Method 3:
    //Using append function
    string address = street.append(house);
    cout<<address<<endl;
    return 0;
}

Note : If you have numbers in your string, they will not sum up, rather they will be concatenated.


string.cpp
#include<iostream>
#include<string>
using namespace std;

int main(){
    string nums = "10" + "20";
    cout<<nums<<endl;
    return 0;
}

String Length:

Length of string can be found by using ".length()" or ".size()" method.

string.cpp
#include<iostream>
#include<string>
using namespace std;

    int main(){
    string name = "Joseph";
    cout<<"Length of name: " << name.length();
    //You can also use name.size() 
    return 0;
}

Accessing Characters in string:

As we have previously discussed that string is a sequence of characters. So we can access individual character and we can also reassign them some other values.

string.cpp
#include<iostream>
#include<string>
using namespace std;

int main(){
    string name = "Joseph";
    cout<<name[0]<<endl;
    cout<<name[1]<<endl;
    cout<<name[2]<<endl;
    cout<<name[3]<<endl;
    cout<<name[4]<<endl;
    cout<<name[5]<<endl;
    //reassigning value
    name[0] = "H";
    cout<<name<<endl;
    //output will be Hoseph
}

Note: If you want to print double quotes inside string, use \"


string.cpp
#include<iostream>
#include<string>
using namespace std;

int main(){
    cout<<"My name is \"Joseph\".";
    return 0;
}

Inputs in String:

There are two methods of taking inputs in string.

string.cpp
#include<iostream>
#include<string>
using namespace std;

int main(){
    const myName;
    //method 1: 
    cout<<"Enter full name: \n";
    cin>>myName;   
    cout<<"Your full name is: "<<myName<<endl;

    //method 2:
    cout<<"Enter full name: \n";
    getline (cin, myName);   //ignores spaces
    cout<<"Your full name is "<<myName<<endl;
    return 0;
}

Methods of Strings:

In C++, we have to include a header file of string, #include<string>
MethodDescriptionCode
length()It is used to calculate the length of a string variable.
int length = text.length();
append(x)It is used to append strings.
str1.append(str2);
substr(x, y)It is used to find a substring in a string.
string sub = text.substr(0, 5);
replace(x, y, z)It will replace characters from starting index to ending index with the given string.
text.replace(7, 5, "Universe");
erase(x, y)It will remove characters from starting index to ending index.
text.erase(7, 4);
find(x, y)It will search for the first occurrence of the specified substring within the string, starting from the given position.
text.erase(7, 4);