Introduction
std::string is a data type in the C++ programming language used to represent textual data.std::string is a class in the standard template library. It represents sequences of characters, which can be either arrays or pointers to arrays of characters.C++ strings: The Standard string class will explain the concept of C++ strings with many examples.
C++ strings: the std::string
C++ strings have many uses in programming and several features and requirements:
- Add #include <string> at the top of your program
- Add std namespace to the program. The std namespace is a part of the C++ standard library. It is a powerful, general-purpose library for handling data and includes many different kinds of containers.
- The std::string is a dynamic size in the C++ program.
- Deal with input and output stream.
- The class template in the standard library deals with functions to break up complex programs into smaller parts and reduce code redundancy.
- C++ strings can be covered to c-style strings.
- C++ strings are safer compared to c-style strings.
Declaring and initializing C++ strings
Declaring a C++ string is done by specifying the type of data that it will store and the name of the variable.The following code declares a string:
string names[] = {“John”,”Henry”,”Catherine”};string name = {names};string n1 = {“John”,2};//Jostring n2 = {5,’S’};//SSSSS
C++ Assignment
How to use the assignment operator in C++ strings programs?. The assignment operator is “=” and assigns one string to another.The following code indicates how to use the assignment operator in C++ strings programs.
string a ;a = “Hello”;string b = {“World”};a=b;
Concatenate in C++ strings
String concatenation is the procedure of joining two or more strings together into one. It is a fundamental task in programming, and many languages provide a function for this purpose. String concatenation is about constructing up a string from two other strings.
string element1{“This “};string element1{“is”};string all;all = element1 + ” ” + element1 + ” concatenation”;//This is a concatination
Accessing characters
There are three ways to access characters in a string:
- Accessing characters by index.
- Accessing characters by their position in the string.
- Accessing individual chars from a string.
string s {“Sami”};for (char ch: s)cout<<ch<<endl;/*Sami*/
Methods for manipulating C++ strings
C++ strings comparing
C++ string comparing is a way to find out whether two strings are the same or not.
The code of the program
#include <iostream>#include <iomanip>#include <string>using namespace std;int main() {string r {“Red”};string c {“Crimson”};string s {“Salmon”};string p {“Pink”};string t {“Tomato”};string y {“Yellow”};cout<<boolalpha;cout<<r<<” == “<<c<<” : “<<(r==c)<<endl;cout<<endl;cout<<p<<” == “<<p<<” : “<<(p==p)<<endl;cout<<endl;cout<<s<<” == “<<t<<” : “<<(s==t)<<endl;cout<<endl;cout<<c<<” == “<<c<<” : “<<(c==c)<<endl;cout<<endl;cout<<t<<” == “<<t<<” : “<<(t==t)<<endl;cout<<endl;cout<<y<<” == “<<r<<” : “<<(r==y)<<endl;cout<<endl;return 0;}
The output of the program
C++ strings for loop
The for loop is a programming statement that allows the programmer to iterate over a code block. The for loop is the most commonly used type of loop in programming.
1
The syntax for a for loop in c++ is as follows:
for(initialization; condition; increment) {}Let’s take an example of a simple c++ string for loop:
The code of the program
#include <iostream>#include <iomanip>#include <string>using namespace std;int main() {string r {“Red”};string c {“Crimson”};string s {“Salmon”};string p {“Pink”};string t {“Tomato”};string y {“Yellow”};for(size_t i{0};i<r.length();++i)cout<<r.at(i);cout<<endl;for(size_t i{0};i<c.length();++i)cout<<c.at(i);cout<<endl;for(size_t i{0};i<s.length();++i)cout<<s.at(i);cout<<endl;for(size_t i{0};i<p.length();++i) cout<<endl; return 0;}
The output of the program
Substring in C++ strings
A substring in C++ is a sequence of characters that is part of a string. The starting point and length identify the substring.To create a substring in C++, you need to use the substr() function. This function takes two parameters: the string from which you want to extract the substring and the starting point from where you want to start pulling characters. The second parameter indicates the number of characters that we should print.
The code of the program
#include <iostream>#include <iomanip>#include <string>using namespace std;int main() {string g {“Green is my favorite color”};cout<<g.substr(0,5)<<endl;cout<<g.substr(6,2)<<endl;cout<<g.substr(21,5)<<endl;cout<<endl; return 0;}
The output of the program
Erase c++ string
Quick Note Title
Erase C++ string is a function in C++ that removes the content of a string.
The code of the program
#include <iostream>#include <iomanip>#include <string>using namespace std;int main() {string f {“Sami is my first name”};f.erase(0,16);cout<<“f is now: “<<f<<endl;cout<<endl; return 0;}
The output of the program
C++ string getline
The getline() function reads a line from the input stream or from a string.
The getline() function reads a line from the input stream or from a string.
The code of the program
#include <iostream>#include <iomanip>#include <string>using namespace std;int main() {string last_name {};cout<<“Please, enter your last name: “<<endl;getline(cin,last_name);cout<<endl;return 0;}
The output of the program
C++ string find
The find() is a string function in C++ that searches for a given character or string inside the specified range.
The code of the program
#include <iostream>#include <iomanip>#include <string>using namespace std;int main() {string main_sentence(“The word looking for is programming”);string secret_word(“programming”);cout << “Is ” << secret_word << ” a substring of ” << main_sentence << ” ?”<<endl;size_t access_char= main_sentence.find(secret_word);if (access_char == 0)cout << ” Not matched”;elsecout << “Accessing the string after the count of character equal to = ” << access_char << std::endl;return 0;}
The output of the program
Conclusion
As we have seen, there are many benefits to using C++ strings. They can be more easily manipulated and are less prone to errors. The C++ String class provides methods for manipulating strings and performing everyday tasks such as finding lengths, getting substrings, and comparing strings.