String Manipulation
Finding a substring
.find()
- To find a substring in a string we need to use the ".find()" method present in the string class.
-
".find()" either return the index of the first character of the matching or a special value "string::npos".
for example take the following code -
#include<string> #include<iostream> using namespace std; int main() { string name("AnnaBelle"); int pos = name.find("Anna"); if(pos == string::npos) cout << "Anna not found" << endl; else cout << "Anna found at pos : " << pos << endl; return 0; }
The ".find()" method return always the integer value, but a more strict and correct declaration uses "string::size_type"
see below program
#include<string> #include<iostream> using namespace std; int main() { string name("AnnaBelle"); string::size_type pos = name.find("Anna"); if(pos == string::npos) cout << "Anna not found" << endl; else cout << "Anna found at pos : " << pos << endl; return 0; }
.find_first_of
-
It returns the index of the first character of the string that matches
any character of a search string. For example -
above code only find one occurence of the required word.#include<string> #include<iostream> using namespace std; int main() { string numerics("0123456789"); string name("r2d2"); string::size_type pos = name.find_first_of(numerics); cout << "Found numeric at index : " << pos << "\telement is " << name[pos] << endl; }
- Storing all the words of a text-line in a "vector"
.rfind
- It is used to find the last occurence of a substring in a string.
.find_first_not_of
-
Searches for the first character of the string that does not matches with any character of the search string.
like string1.find_first_not_of(string2) returns first element of string 1 that doesn't present in string 2.
.find_last_of
- string1.find_last_of(string2) return index of element last character of string2 matches with any character of string1
.find_not_last_of
- string1.find_last_of(string2) return last character of string1 that isn't present in string2.
Insertion.
Operator (+)
-
Using +(plus Operator), you can easily append some reference string to the main string at the end of the main string.
.push_back
-
It has similar functionality like + operator, that is, you can easily append some string at the end of the main string.
main_string.push_back(reference_string), this will append the reference_string at the end of the
main string.
.insert
-
Insert() operation supports the insertion of extra characters, There are some of the overloading of this funciton shown.
- main_string.insert(position, string), inserts the string at position in the main_string. Here string can be a character, a c-string, a string etc.
- main_string.insert(pos, str, str_start_pos, str_end_pos), inserts a string str from str_start_pos upto length str_end_pos in string main_string at position pos.
To learn more about the string insertion click here.
.append
-
It is used to append a particluar portion of one string to another string. Overloadings are shown below.
- main_string.append(string, len), appends the len number starting from index 0 of characters from string to the main_string.
- main_string.append(string), it directly appends string at the end of main_string.
To learn more about append click here.
.assign
-
It is used to assign a portion of one string to another string. Below are overloading of this function.
- main_string.assign(string, len), assigns the first len number of characters of string to the main_string.
- main_string.assign(string, start_pos, len), assigns the len number of characters of string starting from position start_pos, to the main_string.
To read more about String::assign clic here.
Erasing.
.erase
-
word.erase(position, characters), here in the erase the first arguement, tells
from where to start erase and
second arguement tells how many elements to erase. Some of the overloading of erase given below.
- .erase(position), erases the character present at that posiiton.
- .erase(start pos, end pos), erases the character between start position and ending position wihtout erasing the character at ending position. It is used with the help of iterators.
- .erase(start position), erases the characters from starting position up to the end of the string, It is used with the help of iterators.+
.pop_back
-
It simply remove the last character of the main_string. example :- suppose main_string = "Yourcpptutor.com", then you use
main_string.pop_back(), the string then become "Yourcpptutor.co".
Replacing.
.replace
- string1.swap(string2), directly swaps the two strings.
To learn more about comparing two strings click here.
We can even use relation operators to compare two strings.
.swap
- .replace(), provides us with ten overloadings.
- main_string.replace(star_pos, len, string), replace the len number of characters from main_string with string.
- main_string.replace(start_pos, len, string, string_st_pos, len_in_string, replaces the len characters of main_string starting from position start_pos with len_in_string number of characters in string starting from string_st_pos.
- main_string.replace(start_pos, len, string, n), replaces the len characters of main_string starting at start_pos with n*string.
To read more about replace click here.
Comparing.
.compare
-
There are following results that can be returned by string1.compare(string2).
- if string1 is greater than string2, compare() returns a positive value.
- if string2 is greater than string1, compare() return a negative value.
- if string1 and string2 are equal, compare() return zero.
Comments
Post a Comment