Learn more about String Class in C++ Programming

0
1101

C++(20)-Strings---(5)

Continuing with Strings. In this article, we are going to look at more examples on using the string class. Have a nice reading.

Initializing a string Object using another string Object

Besides the two forms we learned in the last article, a string object can also be initialized by assigning another string object to it. So if:

The following will assign x to another string object y:

String Concatenation

The string class has overloaded the plus + operator to concatenate string objects.

Example
The following program will append a string to the end of another.

When executed, the program should display the following output:
1

Now, let’s explain:

  • The statement:

appends a comma and white space to the string object str1.

  • The statement:

also appends a string to the end of another. It has the same effect of writing

Reading Input for string Objects

The getline() function could be used to read one or more lines of input. Among many overloaded forms of syntax available, the function has the following two commonly-used syntaxes:

Where:
cin is the standard input stream object.
STRINGOBJ is the name of the string object to assign the input to.
DELIMITER_CHAR specifies the character which when encountered, will stop the function from reading input.

Example
This example illustrates the use of two forms mentioned above for the getline() function. Read the following code, and copy it into your Dev-C++ IDE:

When executed, the program should behave like this:
2

As you can see, the statement getline(cin, name); reads a single line of input. On the other hand, the statement getline(cin, address, ‘#’); will continue to read input (that may span several lines) until it encounters the character #.

Obtaining the Length of the string Object

The length() method returns the number of characters in a string object.

So, if:

and

The variable len will have a value of 6.

The clear() Method

As its name implies, the clear() method erases the contents of a string object, leaving an empty string behind.

Again, if:

the statement

will clear the name object.

Learn the Basics of C Programming Language

The erase() Method

The erase() member function removes part of a string object. The part to remove is specified by the starting character position and the number of characters to remove.

Example
The following program illustrates the use of the erase() function.

When executed, the program will print the following output:
3

The function call:

will erase four characters starting from the first (at position 0).

The replace() Function

This function replaces a part of the string (identified by starting position and length) by another string, or fills its place by a number of consecutive copies of a character.

Example
The following example illustrates how the replace() function works.

When executed, the program will replace the last name of my favorite player by “Nesta”.
4

Retrieving a Substring of string Object

The substr() function returns a substring of a string object.

Syntax

Example

Given:

The following statement returns a substring of six characters length, starting from the fifth character:

Summary

In this article, we continued to talk about the standard string class.

  • The plus + operator has been overloaded to concatenate two string objects.
  • The += operator can be used to append a string to a string object.
  • The getline() function is used to read one or more lines of input.
  • The length() method returns the number of characters in a string object.
  • The clear() member function erases the contents of a string object, leaving an empty string behind.
  • The erase() member function removes part of a string object.
  • The substr() function returns a substring of a string object.

Before you get bored of this long talk about Strings, let me tell you that the next article will cover a new subject that is another interesting topic: Operator Overloading.

See you.

LEAVE A REPLY

Please enter your comment!
Please enter your name here