C++ String Operators
String in C++ Standard Library Simplified - Part 4
Forward: In this part of the series, we look at the C++ String Operators.
By: Chrysanthus Date Published: 25 Aug 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
The String + Operator
You use the operator + to concatenate two strings. The syntax is:
str = str0 + str1;
The following example illustrates this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str0 = "This is the first sentence.";
string str1 = " This is the second sentence.";
string str = str0 + str1;
cout << str;
return 0;
}
The String == Operator
This is the string equality operator. It checks if two strings are equal in number of characters, particular characters and position of the characters. You would normally use this in conditionals as in the following example:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str0 = "This is the first sentence.";
string str1 = "This is the first sentence.";
if (str0 == str1)
{
cout << "The strings are equal.";
}
return 0;
}
The String != Operator
This is the Not Equal To operator for strings. This is the opposite of ==. You would normally use this in conditionals as in the following example:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str0 = "This is the first sentence.";
string str1 = " This is the second sentence.";
if (str0 != str1)
{
cout << "The strings are not equal.";
}
return 0;
}
Actually the == and != do alphabetical equality and alphabetical non-equality respectively.
The dictionary arranges words alphabetically. The < operator compares two strings alphabetically similar to the logic that is used with the dictionary. A word that comes first in the dictionary is less than the word that comes next in the dictionary. < is normally used in conditionals. If the left string is smaller (dictionary-wise) than the right string, 1 for true is returned; while if the right string is smaller than the left string, 0 is returned for false. Read and try the following:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str0 = "man";
string str1 = "map";
if (str0 < str1)
{
cout << "str0 is smaller than str1.";
}
return 0;
}
The String > Operator
The > operator compares two strings alphabetically similar to the logic that is used with the dictionary. > is normally used in conditionals. If the left string is bigger (dictionary-wise) than the right string, 1 is returned; while if the right string is bigger than the left string, 0 is returned. Read and try the following example:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str0 = "man";
string str1 = "map";
if (str1 > str0)
{
cout << "str1 is bigger than str0.";
}
return 0;
}
The <= operator compares two strings (dictionary-wise). If the left string is smaller than or equal to the right string, 1 is returned; while if the right string is smaller than or equal to the left string, 0 is returned. Read and try the following:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str0 = "man";
string str1 = "man";
if (str0 <= str1)
{
cout << "str0 is smaller or equal to str1.";
}
return 0;
}
The String >= Operator
The >= operator compares two strings (dictionary-wise). If the left string is bigger than or equal to the right string, 1 is returned; while if the right string is bigger than or equal to the left string, 0 is returned. Read and try the following:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str0 = "man";
string str1 = "man";
if (str0 >= str1)
{
cout << "str0 is bigger or equal to str1.";
}
return 0;
}
The String swap Operator
The swap operator is like a function call. It swaps the contents (literals) of two string objects. One of the strings is the object of the swap operator and the other is the argument. Read and try the following code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str0 = "man";
string str1 = "woman";
str0.swap(str1);
cout << str0 << "\n";
cout << str1 << "\n";
return 0;
}
Well, we have seen the main points concerning C++ string operators. Let us take a break here and continue in the next part of the series.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT