Basic Functions of String
We already know how functions make our work so simple and organized. Read the 'functions' module if you haven't done so yet. Now we have various functions specific to a string data type that can greatly ease our programming when dealing with strings.
To use these functions we need to import a string
header file as the definitions of these functions are provided inside these files.
The functions that we are going to study in this article are:
length()
- finds the length of a stringinsert()
- inserts characters into an existing stringsubstr()
- generates a substring from a stringswap()
- swaps values of two string variables- String concatenation
+
- joins two string
Now let's see what these functions are:
'length()' function
As the name suggests, length()
function is used to find the length of the given string.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string text = "String Functions Learnyard";
cout << "string = " << text << endl;
// text.length() returns the length of string text
cout << "string length = " << text.length();
return 0;
}
Output
string = String Functions Learnyard
string length = 26
Here, #include <string>
imports the header file for using string functions and text.length()
finds the length of the string named 'text'.
'insert()' function
Suppose there are two strings abcdef
and lmnopq
. Now I want to insert the second string into the first one after c like: abclmnopqdef
. This is what insert()
function will help us to achieve.
insert()
function of string enables us to insert one string into another string variable. Let's see how.
PS: Don't forget to include the string
header file.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string text1 = "Hello";
string text2 = "World ";
// insert text2 to index 2 of text1
text1.insert(2, text2);
cout << text1;
return 0;
}
Output
HeWorldllo
Quiz
What will be the output of the following code?
#include <iostream>
#include <string>
using namespace std;
int main() {
string sentence = "The fox";
sentence.insert(2, "red");
cout << sentence;
return 0;
}
'substr()' function
Suppose we have a string as: The painting is beautiful
and I want to extract the word painting
from this. Might sound a bit complex if done manually but substr() function simplifies this task for us!
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string text = "The painting is beautiful;";
// extract 8 characters from index 4 of text
string sub_string = text.substr(4, 8);
cout << sub_string;
return 0;
}
Output
painting
Here,
include <string>
imports the header file for string functions
string sub_string = text.substr(4, 8);
: This line creates a new string named 'sub_string' that stores the substring of text. To define the range of this substring, we pass the starting index of the substring that we want to create and the length of the substring from the starting index in the substr() function.
In this example, painting
starts from 4th index in text and has a length of 8. So we pass these two values in substr() function upon text.
Quiz
What will be the output of the following code?
#include <iostream>
#include <string>
using namespace std;
int main() {
string text = "Green Grass";
string sub_string = text.substr(5, 5);
cout << sub_string;
return 0;
}
'swap()' function
Very self-explanatory term indeed! We use the swap()
function to swap two strings. Let's see an example of the same.
Example
#include <iostream>
using namespace std;
int main() {
string text1 = "Happy";
string text2 = "Sad";
cout << "Before Swapping:" << endl;
cout << "text1 = " << text1 << endl;
cout << "text2 = " << text2 << endl;
// swap text1 and text2
text1.swap(text2);
cout << "\nAfter Swapping:" << endl;
cout << "text1 = " << text1 << endl;
cout << "text2 = " << text2;
return 0;
}
Output
Before Swapping:
text1 = Happy
text2 = Sad
After Swapping:
text1 = Sad
text2 = Happy
Concatenation of strings
Suppose there are 2 strings as aero
and plane
. We want to join them to achieve a string as aeroplane
. To achieve this, we can simply use a +
operator.
+
operator is used to concatenate (join) two strings.
Example
#include <iostream>
using namespace std;
int main() {
// create a string
string text1 = "Aero";
string text2 = "plane";
// join string
string joined_string = text1 + text2;
cout << "Joined string: " << joined_string << endl;
cout << "Individual strings: "<< text1 + " " + text2;
return 0;
}
Output
Joined string: Aeroplane
Individual strings: Aero plane
Quiz
What will be the output of the following code?
#include <iostream>
using namespace std;
int main() {
string word1 = "Sun";
string word2 = "Sets";
string word3 = "In The";
string word4 = "West";
cout << word1 + " " + word2 + word3 + word4;
return 0;
}
With this, we come to the end of the string module. We have learned a lot of concepts about strings but there's a lot more to learn while practicing these topics. Meet you in the practice section of strings : ) Till then don't feel shy to re-read these articles in case of any confusion or doubts xD