Broad Network


C++ String Modifiers

String in C++ Standard Library Simplified - Part 2

Forward: In this part of the series, we look at C++ string modifiers.

By: Chrysanthus Date Published: 25 Aug 2012

Introduction

This is part 2 of my series, String in C++ Standard Library, Simplified. The C++ Class has 1 operator and 7 member functions that can be used to modify a string. In this part of the series, we look at C++ string modifiers.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The += Append Operator
Adding a bit of string to your string of interest (main string) is called appending. You can use the += operator for that: The following code illustrates this:

#include <iostream>
#include <string>
using namespace std;

int main()
    {
        string str0 = "Yes, I am the one.";
        string str1 = " It is true.";

        str0 += str1;
    
        cout << str0;

        return 0;
    }

The append Member Function
If you do not want the above operator, you can use the member function (method), append. In its simple form, the bit of string to be appended goes as an argument to the append function. The resulting main string is longer. The following code illustrates this:

#include <iostream>
#include <string>
using namespace std;

int main()
    {
        string str0 = "Yes, I am the one.";
        string str1 = " It is true.";

        str0.append(str1);
    
        cout << str0;

        return 0;
    }

Note the use of the dot operator between str0 and append().

The assign Member Function
You can have the literal of one string copied to replace the literal of another string, using the assign member function. The following code illustrates this:

#include <iostream>
#include <string>
using namespace std;

int main()
    {
        string str0 = "Yes, I am the one.";
        string str1 = "It is true.";

        str0.assign(str1);
    
        cout << str0;

        return 0;
    }

The insert Member Function
You can insert a second string into a first string. In simple terms the syntax is:

    insert(index, 2ndStr)

The following code illustrates this:

#include <iostream>
#include <string>
using namespace std;

int main()
    {
        string str0 = "I you.";
        string str1 = "really miss ";

        str0.insert(2, str1);
    
        cout << str0;

        return 0;
    }

Remember, index counting begins from zero.

Erase a Portion of a String
You can erase the portion of a string, You use the erase member function. In simple terms the syntax is:

    erase(startPos, portionLength)

If the portionLength argument is omitted, it erases to the end of the string. If the portionLength and startPos are omitted, it erases the whole string. In the following code, the start position, is 5 and the portion length is 11. Try it.

#include <iostream>
#include <string>
using namespace std;

int main()
    {
        string str0 = "Yes, it is true I am the one.";

        str0.erase(5, 11);
    
        cout << str0;

        return 0;
    }

Note: In the string class, counting of the characters in the string literal begins from zero.

Replace a Portion of String
You can replace a portion of a string with some other string. You use the replace member function. In simple terms the syntax is:

        replace(startPos, length, replacement)

So you have two strings. In the string of interest, the position where you want the replacement to start is the first argument. The second argument is the length of the portion you want to remove from the string of interest. The third argument is the string that will replace the portion. The following code illustrates this:

#include <iostream>
#include <string>
using namespace std;

int main()
    {
        string str0 = "I have some pens in my bag.";
        string str1 = "a very big book";

        str0.replace(7, 9, str1);
    
        cout << str0;

        return 0;
    }

Hey, a string as argument can have the literal string instead of the identifier. The following code illustrates this:

#include <iostream>
#include <string>
using namespace std;

int main()
    {
        string str0 = "I have some pens in my bag.";

        str0.replace(7, 19, "a very big book");
    
        cout << str0;

        return 0;
    }

Copy a Portion of a String into an Array
You can copy a portion of a string into an array using the member function, copy(). In simple term, the syntax is:

        copy(charArray, rlen, startPos)

The portion of the string of interest begins from startPos. The length of the portion is rlen. The array into which the portion is copied is charArray. The function returns rlen, which is the length of the portion copied.

The array charArray does not end up with the terminating null character. Its length should be at least rlen. The elements of the array are replaced. The following code illustrates the use of the copy member function:

#include <iostream>
#include <string>
using namespace std;

int main()
    {
        string str = "Yes, I am the one. It is true; I did it.";

        char arr[12];
        int len = str.copy(arr, 12, 19);
    
        cout << len << "\n";
        for (int i=0; i<12; ++i)
            {
                cout << arr[i];
            }

        return 0;
    }

Swapping the Contents of Two Strings
You can swap the contents (literals) of two strings using the member function, swap(). In simple terms the syntax is:

        swap(str1)

The content of the string of interest, str0 say, is swapped with the content of str1, as illustrated in the following code:

#include <iostream>
#include <string>
using namespace std;

int main()
    {
        string str0 = "He is a big boy.";
        string str1 = "She is a big girl.";

        str0.swap(str1);
    
        cout << str0 << "\n";
        cout << str1 << "\n";

        return 0;
    }

Well, we have seen C++ string modifiers. Let us take a break here and continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message