The Standard Template Library (STL) in C++ gives us a wide range of powerful tools and data structures to simplify our programming tasks. String class is one of the most commonly used components in the STL.Â
In this blog post, we will go through String class in STL, exploring its features, functionalities, and demonstrating code examples to highlight its usefulness.
Overview of the String Class:
String class in the STL provides a convenient way to handle and manipulate strings of characters in C++. It offers a rich set of member functions and operators to perform various functions on strings like concatenation, comparison, searching, and more.
1. The string class is a specialization of a more general template class called basic_string.
2. Since defining a class in C++ is creating a new datatype, string is derived datatype.
3. This means operators can be overloaded for the class.
4. String is safer than char array. It is container class in STL.
5. In order to use string class, you have to include string header in the header section of the programme. #include<string>
Creating String Objects:
#include<iostream>
#include<string>
using namespace std;
int main() {
string myString = "Hello, world!";
return 0;
}
Basic Operations:
The String class supports various basic operations for manipulating strings. Here are a few examples:Concatenation: Strings can be concatenated using operators like '+' or '+='.Â
Length: The length() or size() member functions provide the length of the string.
String Comparisons:
The string class enables comparison operations between strings using operators like '==', '!=', '<', '>', '<=', and '>='. We will demonstrate how to compare strings using these operators and the member function 'compare()'. These functions return a value based on the comparison of the strings.
Input and Output:
#include<iostream>
using namespace std;
int main() {
string myString;
cout << "Enter your name: ";
cin >> myString;
cout << "Hello, " << myString << "!" << endl;
return 0;
}
String Initialization and Assignment:
The string class provides multiple ways to initialize and assign values. Examples include initializing with a string literal, another string object, or a character array. This blog will demonstrate different initialization methods that are used in string class.
String Operations:
The string class provides a multitude of operations for string manipulation. Below are some commonly used operations of string:
- Appending and concatenating strings.
- Accessing individual characters.
- Modifying strings using insertions, deletions, and replacements.
- Finding substrings and searching for specific characters.
- Converting strings to uppercase or lowercase.
- Getting the length and capacity of a string.
String Input and Output:
The string class seamlessly integrates with input/output streams, allowing easy input and output operations. This blog will show how to read strings from the user and write strings to the console or file using standard stream objects.
Performance Considerations:
We will briefly touch upon the performance considerations when using the string class, such as avoiding unnecessary string copies and utilizing move semantics when appropriate.
Constructors in String class in STL in C++
- string()
- string(const char *str)
- string(const string &str)
#include<iostream>
#include<string>
using namespace std;
int main() {
string S1;
string S2 = "HELLO";
// string S2("HELLO");
// string S2 = string("HELLO");
string S3(S2); // string S3 = S2;
return 0;
}
Operators in String:
- = : assignment operator
- + : concatenation operator
- += : concatenation assignment operator
- == : equality operator
- != : inequality operator
- < : less than operator
- <= : less than or equal operator
- > : greater than operator
- >= : greater than or equal operator
- [] : subscripting operator
- << : insertion (used for output) operator
- >> : extraction (used for input) operator
#include<iostream>
#include<string>
using namespace std;
int main() {
string myStr;
myStr = "Hello "; //assignment operator
string myStr2 = "Students";
myStr2 = myStr+myStr2; //concatenation operator
myStr += myStr2; //concatenation assignment operator
//myStr = myStr+myStr2;
if (myStr == myStr2)
return 0;
cout << myStr << myStr2 << endl;
cout << "Enter your Name";
string myStr3;
cin >> myStr3;
cout << "Hello," << myStr3;
char string1[] = "Students";
string myStr4 = myStr1 + string1; //mixed operations
cout << myStr4; // Output: Hello Students
return 0;
}
Useful Methods of String class in C++:
- assign() : used to assign new value to a string
- begin() : used to return first character's reference
- append() : appends the first string to the second string
- insert() : used to insert a new character
- replace() : used to replace a portion of string
- erase() : used to remove the characters
- find() : used to find the string or character
- rfind() : used to search for the string's last occurence
- compare() : used to compare two strings
- c_str() : used to return pointer to an array
- size() : used to find the string length in terms of bytes
#include<iostream>
#include<string>
using namespace std;
int main() {
string S1;
S1.assign("Hello");
cout << S1; //Output: Hello
S1.append("Students");
cout << S1; //Output: Hello Students
string S2 = "Hello";
S2.insert(2, "123");
cout << S2; //Output: He123llo
S2.replace(2,2,"N");
cout << S2; //Output: HeA3llo
S2.erase(); //delete all strings element
string M = "Hello Online Students";
int i = M.find("online");
cout << i; //Output: 6
cout M.find("NKS"); //Output: -1 (it means not found)
string N = "Hello online online students";
cout << N.rfind("online"); //Output: 13
string som = "Alexander";
string suo = "Felarus";
int i = som.compare(suo);
cout << i; //Output: -1
string N1 = "Hello";
char str[20];
strcpy(str, N1.c_str());
cout << str; //Output: Hello
int i = N1.size();
cout << i; //Output: 5
return 0;
}
The string class in the C++ Standard Template Library (STL) provides a powerful and efficient way to handle string manipulation in C++.
Its intuitive member functions and operators simplify common string operations. By leveraging the features and functionalities of the string class, programmers can efficiently work with strings in their C++ projects.
Note: Remember to include the <string> header in your code to access the String class and its functionalities. By mastering the String class, you will have a powerful tool at your disposal for working with strings in C++ with ease.