Broad Network


C++ String Operations

String in C++ Standard Library Simplified - Part 3

Forward: In this part of the series, we look at the C++ String Operations.

By: Chrysanthus Date Published: 25 Aug 2012

Introduction

This is part 3 of my series, String in C++ Standard Library Simplified. In this part of the series, we look at the C++ String Operations.

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.

Returning a Core String with Constant Content
You can instantiate a string object from a string class. The literal of this string object can be copied into a const char* pointer, making the content (values) copied, constant. The string member function, c_str() is used. With this function the null terminating character is also copied. The following code illustrates this:

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

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

        const char *ptrChar = str0.c_str();
    
        cout << ptrChar;

        return 0;
    }

The right hand side in the second statement in main does the copy using the c_str() member function. Note the use of the dot operator between the string object str0 and the function call. The left hand side declares an array of chars with constant values, using pointer notation.

Note: an array whose values are constant is a special kind of array; it is not the conventional array, where it is the pointer that is constant.

Note: in the second statement in main or in a similar statement the size of the array, ptrChar, is, str0.size() + 1. The string member function, size() gives you the size of the literal string without including the null terminating character. Since ptrChar is just an array of chars and not really a string instantiated from the string class, its array size has to include the null terminating character, which has been copied into the array.

Note: The content of characters of a string object is not constant.

Position of Sub-string in a String
You can find the position of a sub-string in a main string. You use the find function. The returned position value can be assigned to an int. Read and try the following code:

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

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

        int pos = str0.find("am the");
    
        cout << pos;

        return 0;
    }

In this code the main string is "Yes I am the one." and the sub-string is "am the".  The returned position value is 6, which is the beginning of the sub string.

Arguments: the find method (function) of the string class can take two arguments. The first argument is the sub string; the second argument, which is optional, is the position in the main string from where you want search to begin. Remember: counting chars in a string begins from zero. The following code illustrates the use of the second argument and the returned value is –1, which indicates that the sub-string was not found:

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

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

        int pos = str0.find("am the", 7);
    
        cout << pos;

        return 0;
    }

From the code we see that the sub-string begins from index 6, but the second argument is 7.

Find from Reverse Direction
You can find the position of a sub string from the end of a main string. You use the rfind method whose syntax is similar to that of the find method. The following code illustrates this:

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

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

        int pos = str0.rfind("Yes");
    
        cout << pos;

        return 0;
    }

In this code, there are two “Yes” in the main string. It is the last one that is found, which begins from index 18 (counting from left). The second argument can be used as illustrated in the following code:

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

int main()
    {
        string str0 = "Yes I am the one. Yes I did it.";
        
        int pos = str0.rfind("Yes", 15);
        
        cout << pos;
        
        return 0;
    }

You may be surprised to discover that in this code it is the first Yes that is found. The rfind function searches the sub-string from end of the main string. The second argument is measurement from the beginning (left) of the main string. The second argument tells the string object to start search beyond its index. However, with the rfind function, beyond means in front (left) of the index (15).

Index within Sub String to be found
Consider the following code:

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

int main()
    {
        string str0 = "Yes I am the one. Yes I did it.";
        
        int pos = str0.find("Yes", 15);
        
        cout << pos;
        
        return 0;
    }

The returned index value is 18. The second argument in the find method is 15, so the search started at the 15th position with zero based counting in the main string. So the second “Yes” was matched. The second “Yes” in the main string occupies the indices, 18, 19 and 20. What if the second argument of the find method was any of these numbers: 18, 19 or 20? In this situation the sub-string would be found only in the case of 18. So the search is found only in the case where the second argument in the find method is the index of the first character for the sub-string in the main string.

That was for the find method. For the rfind method if the number for the second argument is less than 18 then the second “Yes” (beginning from left) will not be found.

Case Sensitivity of Search
For the find and rfind functions, the search is case sensitive. For example, ‘yes’ would not be seen as ‘Yes’.

The following code returns –1 meaning the search item was not found:

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

int main()
    {
        string str0 = "Yes, I am the one.";
        
        int pos = str0.find("yes");
        
        cout << pos;
        
        return 0;
    }

First and Last Occurrence
The sub-string searched may occur more than once in the main string. The find method searches the first occurrence of the sub-string in the main string. The rfind method searches the last occurrence of the sub-string in the main string. If you want to search beyond that you have to use the second argument of the find or rfind method.

Search not Found
When a search is found the return index is a positive integer beginning from zero. If no search is found –1 is returned.

Subtract a Sub-String and Return
You can subtract a sub-string from a main string with the subst method and return it. The main string remains unchanged. Read and try the following code that illustrates this:

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

int main()
    {
        string str0 = "You have 100,000 dollars.";
        
        string strPiece = str0.substr(9, 7);
        
        cout << strPiece;
        
        return 0;
    }

The method for this subtraction is substr(). It takes 2 arguments, which are positive integers. The first argument is the starting index (zero based) for the sub-string in the main string and the second is the length in chars for the sub-string.

The first argument is optional. If you omit the first argument, you have to also omit the comma that follows it. When the first argument is omitted, the subtraction begins from index zero.

Well, we have seen the main points concerning C++ string operations. 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