In C++ we can store string by one of the two ways –
- C style strings
- string class (discussed in this post)
In this post, second method is discussed. string class is part of C++ library that supports a lot much functionality over C style strings.
C++ string class internally uses char array to store character but all memory management, allocation and null termination is handled by string class itself that is why it is easy to use. The length of c++ string can be changed at runtime because of dynamic allocation of memory similar to vectors. As string class is a container class, we can iterate over all its characters using an iterator similar to other containers like vector, set and maps, but generally we use a simple for loop for iterating over the characters and index them using  operator.
C++ string class has a lot of functions to handle string easily. Most useful of them are demonstrated in below code.
first string first string ##### string first Length of string is : 6 third character of string is : r First char is : s, Last char is : g string string extension string str4 found in str6 at 0 pos ext extension string nsion strinion These are test examples
As seen in above code, we can get length of string by size() as well as length() but length() is preferred for strings. We can concat a string to another string by += or by append(), but += is slightly slower than append() because each time + is called a new string (creation of new buffer) is made which is returned that is a bit overhead in case of many append operation.
On basis of above string function some application are written below :
Floating part is : 342 string contains only digit google%20com%20in
- How to quickly reverse a string in C++?
- C++ String Class and its Applications | Set 2
- Array of Strings in C++
- Converting string to number and vice-versa in C++