C++ Standard Library: The string Class
The string class is part of the C++ standard library. A string represents a sequence of characters.To use the string class, #include the header file:
#include <string>
Constructors:
- string ()
- creates an empty string (""
) string ( other_string )
- creates a string identical toother_string
string ( other_string, position, count )
- creates a string that containscount
characters fromother_string
, starting atposition
. Ifcount
is missing (only the first two arguments are given), all the characters fromother_string
, starting atposition
and going to the end ofother_string
, are included in the new string.string ( count, character )
- create a string containingcharacter
repeatedcount
times
string s1; // s1 = "" string s2( "abcdef" ); // s2 = "abcdef" string s3( s2 ); // s3 = "abcdef" string s4( s2, 1 ); // s4 = "bcdef" string s5( s2, 3, 2 ); // s5 = "de" string s6( 10, '-' ); // s6 = "----------"The string class also has a destructor that takes care of freeing the memory storing the characters when the object is destroyed.
Constant Member Functions:
These functions do not modify the string.const char * data ()
- returns a C-style null-terminated string of characters representing the contents of the stringunsigned int length ()
- returns the length of the stringunsigned int size ()
- returns the length of the string (i.e., same as the length function)bool empty ()
- returnstrue
if the string is empty,false
otherwise
Operators Defined for string
:
- Assign
=
string s1;
string s2;
...
s1 = s2; // the contents of s2 is copied to s1 - Append
+=
string s1( "abc" );
string s2( "def" );
...
s1 += s2; // s1 = "abcdef" now - Indexing
[]
string s( "def" );
char c = s[2]; // c = 'f' now
s[0] = s[1]; // s = "eef" now - Concatenate
+
string s1( "abc" );
string s2( "def" );
string s3;
...
s3 = s1 + s2; // s3 = "abcdef" now - Equality
==
string s1( "abc" );
string s2( "def" );
string s3( "abc" );
...
bool flag1 = ( s1 == s2 ); // flag1 = false now
bool flag2 = ( s1 == s3 ); // flag2 = true now - Inequality
!=
- the inverse of equality - Comparison
<, >, <=, >=
- performs case-insensitive comparisonstring s1 = "abc";
string s2 = "ABC";
string s3 = "abcdef";
...
bool flag1 = ( s1 < s2 ); // flag1 = false now
bool flag2 = ( s2 < s3 ); // flag2 = true now
Member Functions:
void swap ( other_string )
- swaps the contents of this string with the contents ofother_string
.string s1( "abc" );
string s2( "def" );
s1.swap( s2 ); // s1 = "def", s2 = "abc" nowstring & append ( other_string )
- appendsother_string
to this string, and returns a reference to the result string.string & insert ( position, other_string )
- insertsother_string
into this string at the givenposition
, and returns a reference to the result string.string & erase ( position, count )
- removescount
characters from this string, starting with the character at the givenposition
. Ifcount
is ommitted (only one argument is given), the characters up to the end of the string are removed. If bothposition
andcount
are omitted (no arguments are given), the string is cleared (it becomes the empty string). A reference to the result string is returned.unsigned int find ( other_string, position )
- findsother_string
inside this string and returns its position. Ifposition
is given, the search starts there in this string, otherwise it starts at the beginning of this string.string substr ( position, count )
- returns the substring starting atposition
and of lengthcount
from this string
No comments:
Post a Comment