Skip to main content
String manipulations

String Manipulation

Finding a substring

.find()

  1. To find a substring in a string we need to use the ".find()" method present in the string class.
  2. ".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

  1. It returns the index of the first character of the string that matches any character of a search string. For example -

    #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;
    }
    
    above code only find one occurence of the required word.


  2. Storing all the words of a text-line in a "vector"

.rfind

  1. It is used to find the last occurence of a substring in a string.

.find_first_not_of

  1. 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

  1. string1.find_last_of(string2) return index of element last character of string2 matches with any character of string1

.find_not_last_of

  1. string1.find_last_of(string2) return last character of string1 that isn't present in string2.

Insertion.

Operator (+)

  1. Using +(plus Operator), you can easily append some reference string to the main string at the end of the main string.

.push_back

  1. 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

  1. 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

  1. 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

  1. 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

  1. 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

  1. 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

  1. string1.swap(string2), directly swaps the two strings.

  2. To learn more about comparing two strings click here.
    We can even use relation operators to compare two strings.

.swap

  1. .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

Popular posts from this blog

Introduction to Computer Science.

 Before directly jumping deeply in c++, let's first start by creating the roots of computer science. In this post we will answer the following question. What is Computer and How it works? What is Program and Operating system? Low Level vs High Level Language? Compiler v/s Interpreter? By Knowing All these basic concepts You will be able to understand the upcoming more complex concepts easily. Let's start answering the above questions. What is Computer and How it works? What is computer? If I answer briefly,  what is computer?  then it is just a calculator used for doing simple calculations. If I have to answer where is computer used?, then I could probably say everywhere. We are surrounded by computer, for example mobile, smartwatches, and personal computer (obviously).  The below image is the first mechanical computer that we used for calculation.                                   ...

C-style strings vs String Class.

Here, we are going to learn about, what are C-Style Strings and what is String class . So, let's start by giving you the introduction. C-Style String is nothing but, an array of characters, and from the term array we can surely assume that these are static in size i.e. the size of these C-Style strings cannot be increased or decreased. String Class is a Built-in class that provide us much more functionality than C-Style String. C-STYLE STRINGS Before learning the String class, which is full-fledged feature of C++, we should rather start by taking a look at some important things about C-Style String. C-Style Strings are actually array of characters that are terminated by a null character "\0". If you are confused with, why null character? The reason is that, It helps us to define, upto which index we have some useful data present in our character array, and after null character there may or may not be some garbage values of characters. Let's first start by showing a...

Algorithms, Pseduocodes and FlowCharts.

Some Basic Knowledge. C++ is a general purpose programming language, created by Bjarne Stroustrup and was firstly released in  1985. It is an extension to C language, Reason for that is if you are familiar with C language than you must be familiar with the fact that there are no classes, templates etc. present in C language. Due to which when classes are added to C language it is named as "C with Classes", but C++ is considered as default name now.  Extra thing to remember - In "C++" the symbol "++" after C is post increment symbol and therefore, "C++" means that In C additional features are added. Now, let's define, what are algorithms? Algorithms are nothing but set of rules that must be followed to accomplish some task. (Now, What that mean?😕) let's take an example of swapping two numbers, i.e. if a = 10 and b = 20 after swapping a = 20 and b = 10. How will you write the procedure for this?  (There are many such algorithms). Accordin...