Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

08
Dec

C++ Advanced Tutorial - Lesson 8

Related Blog Items

Please refer Lesson 7..

8. CLASS STRING & STRING STREAM PROCESSING
8.1 Basics of string
8.2 String Assignment
8.3 Creating Strings
8.4 Appending Strings
8.5 Comparing Strings
8.6 Sub-string
8.7 Swapping string
8.8 Characteristics of string
8.9 Finding Character in string
8.10 Replacing Characters in a string
8.11 Inserting Characters into a string
8.12 Conversion to C-Style char *
8.13 string Iterators
8.14 String Stream Processing

8. CLASS STRING & STRING STREAM PROCESSING

8.1 Basics of string

string s1("Hello!");     // create a string "Hello!"
string s1 = "Hello!";
string s1(5, 'X');       // create a string "XXXXX"
s1 = 'a';

Constructing a string that is too long will throw a length_error exception.

Unlike C-style string “char *”, string is not required to end with NULL. NULL is regarded as a normal character in string.
Stream insertion and extraction operator (<< and >>) and method getline have been overloaded for string.

8.2 String Assignment

Assignment such as “s1 = s2″ is not allowed for char * but allowed for string, because string is a class and operator = has been properly overloaded. Or you can explicitly call string’s assign method

s1.assign(s2);
s1.assign(s2, start, number);

The second call assigns “number” of characters of s2 to s1, starting from subscript “start”:
A character in a string can be accessed by

s1[2] = 'a';  // or
s1.at[2] = 'a';

[ ] operator doesn’t provide range check, while at method does.

8.3 Creating Strings

There are two ways to create a string:

string name("Frank Liu");
or
string name = "Frank Liu";

The first one is more efficient than the second, becasue the first one only calls class string’s constructor, while the second one calls string’s default constructor first, then its assignment operator.

8.4 Appending Strings

string s1 = "Frank";
string s2 = s1 + " Liu";

s2 will be “Frank Liu”. Or you can explicitly call string’s append method:

s1.append(" Liu"); // or
s1.append(s2, start, end);

The second call appends part of s2 to s1, from subscript “start” to “end”.

8.5 Comparing Strings

The following operation can be done:
==
!=
> & >=
< & <=

Or you can explicitly call string's compare method

int result = s1.compare(s2);

which returns the result as positive if s1 > s2, zero if s1 == s2, negative if s2 < s2.

result = s1.compare(start1, length, s2, start2, length);   // or
result = s1.compare(start1, length, s2);

compare part of s2 with part of s1. s1 starts from “start1″, s2 starts from “start2″, number of characters is “length”.

8.6 Sub-string

cout << s1.substr(start, length);
method substr returns a string made from s1, starting from "start" and number of characters is "length".

8.7 Swapping string

s1.swap(s2);
swaps the value of s1 with s2.

8.8 Characteristics of string

Class string has the following methods to return the characteristics of a string:
capacity returns the number of characters that can be stored in s1 without increasing the memory capacity.
max_size returns the size of the largest possible string that can be stored in a string object.
size and length returns the number of characters currently stored in s1.
empty returns true if s1 is empty.

8.9 Finding Character in string

string::npos is a public static constant defined in class string, representing the maximum string length.

int result = s1.find(”ABC”);

looks for string “ABC” in string s1, and returns the subscript of the location, or string::npos.

result = s1.rfind(”ABC”);
searches string s1 backwards.

result = s1.find_first_of(”ABC”);
searches for the first occurrence in s1 of any character contained in “ABC”. Searching starts from the beginning of s1.

result = s1.find_last_of(”ABC”);
searches for the last occurrence in s1 of any character contained in “ABC”. Searching starts from the end of s1.

result = s1.find_first_not_of(”ABC”);
searches for the first character in s1 which is not contained in “ABC”.

result = s1.find_last_not_of(”ABC”);
searches for the last character in s1 which is not contained in “ABC”.

8.10 Replacing Characters in a string

s1.erase(23);
erases characters form 23 to the end.

s1.replace(start, number, “ABC”);
replaces part of s1 with string “ABC”, starting from “start”, number of characters is “number”.

s1.replace(start1, number1, s2, start2, number2);
replaces part of s1 with part of s2.

8.11 Inserting Characters into a string

s1.insert(before, s2);
inserts s1 into s1 before “before”.

s1.insert(before, s2, start, number);
insert part of s2 into s1 before “before”.

8.12 Conversion to C-Style char *

const char * ptr = s1.data();
returns the content of s1 to ptr, without terminating NULL character. So you must add NULL to the end of string ptr.

s1.copy(ptr, length, 0);

copy s1 to ptr, without terminating NULL character.
s1.c_str() returns a NULL-terminated const char *.

Whenever possible, always use string instead of C-style char *.
Converting a string containing NULL characters to C-style char * may cause logic errors.

8.13 string Iterators

string::const_iterator i1 = s1.begin();
cout << *i;

8.14 String Stream Processing

In addition to standard stream IO and file stream IO, stream IO includes capabilities for inputting from string and output to string in memory. These capabilities are often referred to as in-memory IO or string stream processing.

Input from string is supported by class istringstream, while output by class ostringstream. To use them you must include and .

One application of these techniques is data validation. You first input from standard input stream to a string, then do some modification or validation on it, then input from that string.

string s1 = "Hello! \n";
string s2 = "Good Morning! \n";
string s3 = "How are you? \n";

ostringstream output;
output << s1 << s2 << s3 << endl;
istringstream input("How are you?");
input >> a >> b;

Popularity: 26%

You need to log on to convert this article into PDF


Related Blog Items

No Comments

No comments yet.

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image