C++ String Class and its Applications
C++ string class and its applications have more functions as discussed in this article String vs Character Array In C++, in addition to a character array, there exists a similar kind of way to implement string, that is using a string class which is a part of C++ standard library.
We need to add a header file to implement string using string class. The basic difference between a character array and a string is, in the case of a character array, the size has to be allotted at the time of declaration, i.e all memory once allocated is fixed and cannot be altered at run time. Whereas, for string, there is no need to specify the size and to allocate fixed memory at the time of declaration.
Some useful String Functions
1. compare(string_to_compare ):
It is used to compare two strings. It returns the difference between the second string and the first string in the integer.
Strings are equal
Searches the string for the first occurrence of the substring specified in arguments. It returns the position of the first occurrence of substring.
Searches the string for the first character that matches any of the characters specified in its arguments. It returns the position of the first character that matches.
Searches the string for the last character that matches any of the characters specified in its arguments. It returns the position of the last character that matches.
Searches the string for the last occurrence of the substring specified in arguments. It returns the position of the last occurrence of a substring
First occurrence of "Geeks" starts from : 4 First occurrence of character from "reef" is at : 2 Last occurrence of character from "reef" is at : 16 Last occurrence of "Geeks" starts from : 14
This function inserts the given substring in the string. It takes two arguments, first the position from which you want to insert the substring and second the substring.
This function clears all the characters from the string. The string becomes empty (length becomes 0) after this operation.
Tests whether the string is empty. This function returns a Boolean value.
String is empty
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above