Home - JanKulbaga/String-CPP GitHub Wiki
The String object is used to represent and manipulate a sequence of characters
Strings are useful for holding data that can be represented in text form. Some of the most-used operations on strings are to check their length(), to build and concatenate them using the + or += string operators, checking for the existence of a specific substring with the find() method, extracting substrings with the substring() method or can be transformed into lowercase or uppercase letters using the methods lower() and upper().
Strings can be created with operator= or using the String() constructor:
#include "src/String.h"
int main()
{
String s1 = "Hello World";
String s2 = 'H';
String s3("Hello World");
String s4('H');
return 0;
}There are two ways to access an individual character in a string. The first is the charAt() method and second method is to use an operator []:
#include "src/String.h"
int main()
{
String s1 = "Hello World";
char& c = s1.charAt(1); // gives value 'e'
char& c1 = s[1]; // gives value 'e'
return 0;
}Use relational operators to compare strings:
#include <ostream>
#include "src/String.h"
int main()
{
String s1 = "a";
String s2 = "b";
if (s1 < s2)
std::cout << s1 << " is less than " << s2 << "\n";
if (s2 > s1)
std::cout << s2 << " is greater than " << s1 << "\n";
s1 = "a";
s2 = "a";
if (s1 == s2)
std::cout << s1 << " and " << s2 << " are equal\n";
return 0;
}Output:
a is less than b
b is greater than a
a and a are equal