String Manipulation Functions in C++ programming

By  Falak Hasija    11 - 25 July, 20
string-manipulation-functions-in-c++-programming

Share article on social media


The C++ programming string class has a rich set of string manipulation functions that can be used to manipulate strings.  

ALL THE STRING METHODS ARE FOUND IN STRING LIBRARY  (), are used with the string object and the member operator(.) followed by the function name as shown below.

object.function (...)

Various methods for String manipulation in C++ programming:

1.STRING LENGTH (length and size): The length function returns the length of the string, which is defined as the number of characters in the string. If the string is empty, it returns zero.

There are to methods to determine size/length of a string:

 

                                                                                                   1. len=str1.length();
                                                                                                    2. len=str1.size();

2. STRING COMPARE: To compare string objects, we can use the compare method found in the string library. Strings are compared beginning with the first (leftmost) character and comparing characters until unequal characters are found or until the end of the string is reached.

If unequal characters are found, the strings are unequal. Greater than and less than comparisons determined by comparing the value of unequal characters. If the end of the string is reached, the strings are equal.

NOTE: 1. Comparing a string object and character is not allowed.

                2. While using comparison operators, the result is boolean - TRUE or FALSE.

                3. The results are a negative number (less than), 0 (equal), positive number (greater than).

There are three methods to compare strings:

str1.compare(str2);
str1.compare(pos1, len1, str2);
str1.compare(pos1, len1, str2, pos2, len2 );

pos1, len1 define the substring in str1. pos2, len2 define the substring in str2.

   

 3CONCATENATING AND APPENDING: The joining of two strings to make one long string is called concatenating or appending the strings

CONCATENATION: To join two strings, use plus operator ( + ).

str1 + str2;

APPEND: There are two ways to append strings in C++ programming :


Use the overloaded plus assign operator ( += ).
str1 += str2;

Use a string class method, append. The second string appended to the first string.

(Same as the above method.)

str1.append(str2);

str1.append(str2,pos2,len2);

 

4. EXTRACTING A SUBSTRING: To create a new string from an already existing string, use the substring method to extract a part of a string.

There are three basic formats for the substring extraction method:

str1.substr();
str1.substr(pos1);
str1.substr(pos1,len1);

pos1 is the start position in the string and len1 is the number of characters to be extracted. 

    

5. SEARCHING FOR A SUBSTRING: To know if a substring is in a string, use find method for locating substrings.

There are two find methods in string class:

  • Searching Forwards: The find method: The find method, can be used to search for a substring anywhere in a string starting at the beginning of the string. The basic format is:
where = str1.find (str2, pos1);

str1 is the string to be searched, str2 is an instance of a string object and pos1 is the start position in str1.

  • Search Backwards: The rfind method: The rfind method if used to search for a substring starting at the end of a string and searching toward the beginning of the string. The basic format is:
where = str1.rfind (str2, pos1);

str1 is the string to be searched, str2 is an instance of a string object and pos1 is the start position in str1. 

 

6. STRING INSERTION: The C++ string class has several methods to insert a character, a character a specified number of times, a string, or a substring at a specific position in a string object. 

The basic format of the insert method  is:

str1.insert (pos1, str2);

str1.insert (pos1, str2, pos2, len2);
str1.insert (pos1, numchar, char);

str1 is a string class, pos1 is the insertion index position in the string, str2 is a character/string to be inserted and len2  is either the number of characters to be inserted when str is a character or the length of the string to be inserted when str2 is a string.

7. REPLACE STRING: The string class provides to replace all or part of a string with another string.

The basic formats for the replace method:

str1.replace (pos1, len1, str2);
str1.replace (pos1, len1, str2, pos2, len2);

pos1 is the start position of the characters in str1 to be replaced, and str2 is the replacement value.  

   

8. ERASE STRING: If you want to erase the entire string or part of the string from specified index location, then use the erase method.

The basic formats are:

str.erase (pos, num);
str.clear ();

pos is the start position and num is the number of characters to be erased.

           

 

I hope you get some information regarding string manipulation in C++ programming now.