C++ C++ Tutorial C++ Functions C++ Classes



CPP Strings

C++ is a powerful programming language that provides a wide range of features to developers. One of the most important features of C++ is its ability to handle strings. Strings are a sequence of characters that are used to represent text. In C++, strings are represented using the string class. The string class provides a number of methods that can be used to manipulate strings.

The string class is part of the standard C++ library and is defined in the header file <string>. The string class provides a number of constructors that can be used to create strings. For example, the following code creates a string object:


#include <string>
using namespace std;

int main() {
    string str = "Hello, World!";
    return 0;
}

The above code creates a string object named str and initializes it with the value "Hello, World!". The string class provides a number of methods that can be used to manipulate strings. For example, the following code demonstrates how to use the length() method to get the length of a string:


#include <string>
using namespace std;

int main() {
    string str = "Hello, World!";
    int len = str.length();
    cout << "Length of string is: " << len << endl;
    return 0;
}

The above code creates a string object named str and initializes it with the value "Hello, World!". The length() method is then used to get the length of the string, which is printed to the console.

The string class also provides a number of other methods that can be used to manipulate strings. For example, the append() method can be used to append one string to another:


#include <string>
using namespace std;

int main() {
    string str1 = "Hello, ";
    string str2 = "World!";
    str1.append(str2);
    cout << str1 << endl;
    return 0;
}

The above code creates two string objects named str1 and str2 and initializes them with the values "Hello, " and "World!", respectively. The append() method is then used to append str2 to str1, resulting in the string "Hello, World!" being printed to the console.

In addition to the methods provided by the string class, C++ also provides a number of operators that can be used to manipulate strings. For example, the + operator can be used to concatenate two strings:


#include <string>
using namespace std;

int main() {
    string str1 = "Hello, ";
    string str2 = "World!";
    string str3 = str1 + str2;
    cout << str3 << endl;
    return 0;
}

The above code creates three string objects named str1, str2, and str3 and initializes them with the values "Hello, ", "World!", and the concatenation of str1 and str2, respectively. The resulting string "Hello, World!" is then printed to the console.

In conclusion, C++ provides a powerful string class that can be used to manipulate strings. The string class provides a number of methods that can be used to manipulate strings, and C++ also provides a number of operators that can be used to manipulate strings. By using these features, developers can easily manipulate strings in their C++ programs.

References

Activity