Strings in
C++
By / Mohammed Adel Hassan Amer
()
This is a function to ...
ABC
ABC;
// output : …
()
ABC
ABC;
// output : …
(
)
Access Characters of a String
(by index) …
string s = “Hello World!” ;
cout << s [ 11 ] ;
// output : !
Merge ‘+’
string s = “Hello” ;
s = s + ‘’;
s = s + “World” ;
s = s + ‘\n’ ;
// Hello World
//
Merge ‘+’
s = “” ;
s = “” + ‘x’ ; // error
s = s + ‘x’ ; // ok
s += ‘x’ ; // ok
// output : x
append (
)
Attach char / string to a string ...
string s1 = “Hello” ;
string s2 = “C++” ;
[Link] ( s2 ) ;
// output : HelloC++
append (
)
string s1 = “Hello” ;
string s2 = “C++” ;
// [Link] ( string, index, length ) ;
[Link] ( s2, 1, 2 ) ;
// output : Hello++
append (
)
string s = “Hello” ;
// [Link] ( string, length ) ;
[Link] ( “C++”, 2 ) ;
// output : HelloC+
append (
)
string s = “Hello” ;
// [Link] ( times, character ) ;
[Link] ( 2, ‘C’ ) ;
// output : HelloCC
assign (
)
Assign a Value to a String …
string s ;
[Link] ( “Hello!” ) ;
// s : Hello!
assign (
)
string s1 ;
string s2 = “Hello World!” ;
// [Link] (string, index, length) ;
[Link] ( s2, 0, 5 );
// s1 : Hello!
assign (
)
string s1 ;
string s2 = “Hello World!” ;
// [Link] (string, length) ;
[Link] ( s2, 5 );
// s1 : Hello!
assign (
)
string s ;
// [Link] (times, character) ;
[Link] ( 5, ‘!’ );
// s : !!!!!
at (
)
Access a specific character from
a string ( by Index ) …
string s = “abc” ; string s = “abc” ;
cout << [Link] ( 1 ) ; [Link] ( 1 ) = ‘c’ ;
// b // acc
erase (
)
Erase Characters from a String …
string s = "This is an example sentence.“ ;
[Link] ( 9 ) ;
// output : This is a example sentence.
erase (
)
string s = "This is an example sentence.“ ;
// [Link] ( index, length ) ;
[Link] ( 9, 9 ) ;
// output : This is a sentence.
insert (
)
Put a specific char / string into another …
string s1 = “To be, ” ;
string s2 = “or not to be.” ;
// [Link] ( index , char / string ) ;
[Link] ( 7, s2 ) ;
// To be, or not to be.
insert (
)
string s1 = “To be, ” ;
string s2 = “or not to be.” ;
// [Link] ( index, string, str-index, str-length ) ;
[Link] ( 7, s2, 7, 6 ) ;
// To be, to be.
insert (
)
string s = “To be, ” ;
// [Link] ( index, string, str-length ) ;
In VS-22,
[Link] ( 7, “or not to be.”, 2 ) ; it shows only the
rest of string after
// To be, or str-length
not to be.
insert (
)
string s = “To be or not to be.” ;
char c = ‘,’ ;
// [Link] ( index, times, character ) ;
[Link] ( 5, 1, c ) ;
// To be, or not to be.
insert (
)
string s1 = “To be, ” ;
// [Link] ( ) ;
[Link] ( ) ;
// To be, to be.
push_back (
)
Append a Character to a string ...
string alpha = “ABC” ;
alpha.push_back ( ‘D’ ) ;
// output : ABCD
pop_back (
)
Delete last Character from a string ...
string alpha = “ABCD” ;
alpha.pop_back ( ) ;
// output : ABC
replace (
)
Replace a Part of String with another …
string s = “this is a test string” ;
// [Link] ( index, length, string ) ;
Note that String-2
[Link] ( 8, 6, “really a” ) ; is longer than one
being replaced !
// output : this is really a string (with length 6)
replace (
)
string s = “this is a test string” ;
// [Link] ( index, length, string, index, length ) ;
[Link] ( 10, 4, “a short text”, 2, 5 ) ;
// output : this is a short string
replace (
)
string s = “this is a test !” ;
// [Link] ( index, length, string, length ) ;
[Link] ( 9, 5, “n example”, 9 ) ;
// output : this is an example !
replace (
)
string s = “this is a test .” ;
// [Link] ( index, length, times, char ) ;
[Link] ( 15, 1, 3, ‘!’ ) ;
// output : this is a test !!!
Swap (
)
Swap string values …
string s1 = “Mohammed” ;
string s2 = “Rana” ;
[Link] ( s2 ) ;
// s1 : Rana