Lec-10
Lec-10
String Object
Created By:
Amanpreet Kaur &
Sanjeev Kumar
SME (CSE) LPU
string s = ABCDEFG;
getline(cin, s); //reads entire line of characters
into s
string_characteristics.cpp
String Objects
The C++ string class also defines a length() function for extracting
how many characters are stored in a string.
String Objects
compare.cpp
String Objects
You can also concatenate C++ strings using the + and += operators:
string s = ABCD*FG;
string s2 = Robot;
string s5 = Soccer;
string s6 = s + HIJK; //changes s6 to ABCD*FGHIJK
s2 += s5; //changes s2 to RobotSoccer
8
String Objects
Substring function:
substr()
s6 = ABCD*FGHIJK;
s4 = s6.substr(5, 3); //changes s4 to FGH
String Objects
String Objects
find() function
returns the index of the first occurrence of a given substring:
string s7 = Mississippi River basin; //23 characters
cout << s7.find(si) << endl; //prints 3
cout << s7.find(so) << endl; //prints 23, the length of the string
i.e.
Assignment
Assignment
s2 = s1;
Makes a separate copy
s2.assign(s1);
Same as s2 = s1;
12
Range-checking
Range-checking
s3.at( index );
Returns character at index
Can throw an out_of_range exception
13
catch(exception& e){
cout << "out_of_range exception: " << e.what() << endl;
}
string_characteristics.cpp
Concatenation
Concatenation
s3.append( "pet" );
s3 += "pet";
Both add "pet" to end of s3
14
Comparing strings
Overloaded operators
==, !=, <, >, <= and >=
returns bool
s1.compare(s2)
15
Compare.cpp
Substrings
Function substr gets a substring
s1.substr( start, N );
gets N characters, beginning with index
start
returns substring
16
Swapping strings
s1.swap(s2);
Switch contents of two strings
17
string Characteristics
Member functions
s1.size() and s1.length()
Number of characters in a string
s1.capacity()
Number of elements that can be stored without
reallocation
s1.max_size()
Maximum possible string size
s1.empty()
Returns true if empty
s1.resize(newlength)
18
s1.find( s2 )
s1.rfind( s2 )
Searches right-to-left
s1.find_first_of( s2 )
Returns first occurrence of any character in s2
Example:
19
s1.find_first_of( "abcd" )
Find functions
s1.find_last_of( s2 )
Finds last occurrence of any character
in s2
s1.find_first_not_of( s2 )
Finds first character NOT in s2
s1.find_last_not_of( s2 )
Finds last character NOT in s2
20
others.cpp
Replace
s1.replace( begin, N, s2)
begin: index in s1 to start replacing
N: number of characters to replace
s2: replacement string
21
others.cpp
Example
s1.replace( begin, N, s2, index, num )
begin: index in s1 to start replacing
N: number of characters to replace
s2: replacement string
index: element in s2 where replacement comes
from
num: number of elements to use when replacing
string str = "this is an example string.";
string str3="sample phrase";
str.replace(19,6, str3, 7, 6);
22
23
others.cpp
Summary
C++ strings are safer and easier to use than C string.
24
25
Method
Use
append(char *pt);
append(char *pt, size_t count);
append(string &str, size_t
offset,size_t count);
append(string &str);
append(size_t count, char ch);
append(InputIterator Start,
InputIterator End);
Appends characters to a string from C-style strings, char's or other string objects.
at(size_t offset);
Returns a reference to the character at the specified position. Differs from the
subscript operator, [], in that bounds are checked.
begin();
*c_str();
clear();
empty();
end();
Method
Use
Returns the index of the first character of the substring when found. Otherwise,
the special value "npos" is returned.
find_first_not_of();
Same sets of arguments as find. Finds the index of the first character that is not
in the search string.
find_first_of();
Same sets of arguments as find. Finds the index of the first character that is in
the search string.
find_last_not_of();
Same sets of arguments as find. Finds the index of the last character that is not in
the search string.
find_last_of();
Same sets of arguments as find. Finds the index of the last character that is in the
search string.
push_back(char ch);
Replaces elements in a string with the specified characters. The range can be
specified by a start position and a number of elements to replace, or by using
iterators.
size();
swap(string &str);
26