|
|
|
Create / delete
|
Create string
|
a = "";
|
a = "abcdef";
|
abcdef
|
Initialise
|
Fixed contents
|
a = "";
|
a = "abcdef";
|
abcdef
|
Count
|
Length of string
|
a="abc";
|
i=a.length();
|
3
|
|
Size of underlying storage
|
a="abc";
|
i=a.size();
|
3
|
|
Number of 'X' chars in string
|
a="abcXdefXX";
|
n=count(a .begin(),a.end(),'X');
|
3
|
|
Number of 'X', 'Y' and 'Z' chars in string
|
a="abcXdefYZ";
|
fn_xyz test ; n = count_if(a .begin(),a.end(),test);
|
3
|
Retrieve by index
|
Retrieve 1st char
|
a="abc";
|
c=a[0];
|
a
|
|
Retrieve 1st char
|
a="abc";
|
c=*(a.begin());
|
a
|
|
Retrieve last char
|
a="abc";
|
c=a[a.length()-1];
|
c
|
|
Retrieve last char
|
a="abc";
|
c=*(a.end()-1);
|
c
|
|
Retrieve char 'n'
|
a="abcdef"; n=2;
|
c=a[n];
|
c
|
Slice
|
Slice from index 'm' to 'n' (inclusive)
|
a="abcdefghi"; m=3; n=6;
|
s=a.substr(m,n-m+1);
|
defg
|
|
Slice from index 'n' (inclusive)
|
a="abcdefghi"; n=3;
|
s=a.substr(n);
|
defghi
|
|
Slice up to index 'n' (inclusive)
|
a="abcdefghi"; n=3;
|
s=a.substr(0,n+1);
|
abcd
|
Compare
|
Test for equality (case sensitive)
|
a="abcdef"; b="abc";
|
t=(a==b);
|
0
|
|
Test for prefix
|
a="abcdef"; b="abc";
|
t=(a .compare(0 ,b.length(),b)==0);
|
1
|
|
Test for suffix
|
a="abcdef"; b="def";
|
t=(a .compare(a .length()-b .length() ,b.length(),b)==0);
|
1
|
Search
|
Test char 'is in' string
|
a="abcdefghi"; c='c';
|
t=(a .find(c)!=string::npos);
|
1
|
|
Test substring 'is in' string
|
a="abcdefghi"; b="abc";
|
t=(a .find(b)!=string::npos);
|
1
|
|
Find index of char (forward)
|
a="abcdefccccc"; c='c';
|
i=a.find(c);
|
2
|
|
Find index of char (reverse)
|
a="abcdefccccc"; c='c';
|
i=a.rfind(c);
|
10
|
|
Find index of substring (forward)
|
a="abcdefabc"; b="abc";
|
i=a.find(b);
|
0
|
|
Find index of substring (reverse)
|
a="abcdefabc"; b="abc";
|
i=a.rfind(b);
|
6
|
Split
|
Split on char
|
a="one,two,three"; c=',';
|
sa .clear() ; while (a .length()) { sa .push_back(a .substr(0 , a .find_first_of(c))) ; a .erase(0 ,a .find_first_of(c)) ; a .erase(0 ,a.find_first_not_of(c)); }
|
one two three
|
|
Split on string
|
a="aa_x_bb_x_cc"; b="_x_";
|
sa .clear() ; while (a .length()) { sa .push_back(a .substr(0 , a .find_first_of(b))) ; a .erase(0 ,a .find_first_of(b)) ; a .erase(0 ,a.find_first_not_of(b)); }
|
aa bb cc
|
Insert
|
Prepend char
|
a="bcdef"; c='a';
|
a=c+a;
|
abcdef
|
|
Prepend char
|
a="bcdef"; c='a';
|
a.insert(a.begin(),c);
|
abcdef
|
|
Append char
|
a="abc"; c='d';
|
a+=c;
|
abcd
|
|
Append char
|
a="abc"; c='d';
|
a.insert(a.end(),c);
|
abcd
|
|
Insert char at index 'n'
|
a="abc"; c='X'; n=1;
|
a.insert(a.begin()+n,c);
|
aXbc
|
|
Prepend string
|
a="def"; b="abc";
|
a=b+a;
|
abcdef
|
|
Prepend string
|
a="def"; b="abc";
|
a.insert(0,b);
|
abcdef
|
|
Append string
|
a="abc"; b="def";
|
a+=b;
|
abcdef
|
|
Append string
|
a="abc"; b="def";
|
a.insert(a.length(),b);
|
abcdef
|
|
Insert string at index 'n'
|
a="abcghi"; b="def"; n=3;
|
a.insert(n,b);
|
abcdefghi
|
|
Concatenate 'a' and 'b'
|
a="abc"; b="def";
|
s=a+b;
|
abcdef
|
Overwrite
|
Overwrite char at index 'n'
|
a="abc"; n=1; c='B';
|
a[n]=c;
|
aBc
|
|
Overwrite substring at index 'n'
|
a="abcdefghi"; b="DEF"; n=3;
|
a .replace(n ,b.length(),b);
|
abcDEFghi
|
Replace
|
Replace 1st instance of char 'X' with 'Y'
|
a="XXXYYYZZZ";
|
if ((i=a .find('X'))!=string::npos) a[i]='Y' ;
|
YXXYYYZZZ
|
|
Replace all instances of char 'X' with 'Y'
|
a="XXXYYYZZZ";
|
replace(a .begin() ,a.end(),'X','Y');
|
YYYYYYZZZ
|
|
Replace 1st instance of substring 'abc' with 'XYZ'
|
a="abcdefabc";
|
if ((i=a .find("abc"))!=string::npos) a .replace(i ,strlen("abc") ,"XYZ") ;
|
XYZdefabc
|
|
Replace all instances of substring 'abc' with 'XYZ'
|
a="abcdefabc";
|
while ((i=a .find("abc"))!=string::npos) a .replace(i ,strlen("abc") ,"XYZ") ;
|
XYZdefXYZ
|
|
Replace all instances of 'X','Y','Z' with 'A','B','C' respectively
|
a="XXXYYYZZZ";
|
replace(a .begin() ,a .end() ,'X' ,'A') ; replace(a .begin() ,a .end() ,'Y' ,'B') ; replace(a .begin() ,a.end(),'Z','C');
|
AAABBBCCC
|
Remove
|
Remove 1st instance of char 'X'
|
a="XXXYYYZZZ";
|
if ((i=a .find('X'))!=string::npos) a .erase(i ,1) ;
|
XXYYYZZZ
|
|
Remove all instances of char 'X'
|
a="XXXYYYZZZ";
|
while ((i=a .find('X'))!=string::npos) a .erase(i ,1) ;
|
YYYZZZ
|
|
Remove 1st instance of substring 'abc'
|
a="abcdefabc";
|
if ((i=a .find("abc"))!=string::npos) a .erase(i ,strlen("abc")) ;
|
defabc
|
|
Remove all instances of substring 'abc'
|
a="abcdefabc";
|
while ((i=a .find("abc"))!=string::npos) a .erase(i ,strlen("abc")) ;
|
def
|
Remove by index
|
Remove 1st char
|
a="abcdef";
|
a.erase(0,1);
|
bcdef
|
|
Remove last char
|
a="abcdefghi";
|
a.erase(a.length()-1,1);
|
abcdefgh
|
|
Remove char at index 'n'
|
a="abcdef"; n=3;
|
a.erase(n,1);
|
abcef
|
|
Remove chars in index range 'm' to 'n' (inclusive)
|
a="abcdef"; m=2; n=4;
|
a.erase(m,n-m+1);
|
abf
|
|
Remove chars from index 'n' onwards (inclusive)
|
a="abcdefghi"; n=3;
|
a.erase(3);
|
abc
|
|
Remove chars up to index 'n' (inclusive)
|
a="abcdefghi"; n=3;
|
a.erase(0,n+1);
|
efghi
|
Trim
|
Trim leading white space
|
a=" abc def ";
|
a=a .substr(a .find_first_not_of(' ')) ;
|
abc def
|
|
Trim trailing white space
|
a=" abc def ";
|
a=a .substr(0 ,a .find_last_not_of(' ') +1);
|
abc def
|
|
Trim leading and trailing white space
|
a=" abc def ";
|
a=a .substr(a .find_first_not_of(' '), a.find_last_not_of(' ') - a.find_first_not_of(' ') + 1);
|
abc def
|
|
Trim trailing carriage return
|
a="abcdef\n";
|
if (a.length() > 0 && a[a.length()-1]=='\n') a.erase(a.length()-1,1);
|
abcdef
|
Sort
|
Sort in ascending order
|
a="cafdeb";
|
sort(a.begin(),a.end());
|
abcdef
|
|
Sort in descending order
|
a="cafdeb";
|
sort(a .begin() ,a .end()) ; reverse(a .begin(),a.end());
|
fedcba
|
|
Reverse
|
a="abcdefghi";
|
reverse(a .begin(),a.end());
|
ihgfedcba
|
Convert case
|
Convert to lower case
|
a="ABCDEFGHI";
|
transform(a .begin() ,a .end() ,a.begin(),tolower);
|
abcdefghi
|
|
Convert to upper case
|
a="abcdefghi";
|
transform(a .begin() ,a .end() ,a.begin(),toupper);
|
ABCDEFGHI
|
|
Convert first char to lower case
|
a="ABCDEFGHI";
|
transform(a .begin() ,a .begin() +1,a.begin(),tolower);
|
aBCDEFGHI
|
|
Convert first char to upper case
|
a="abcdefghi";
|
transform(a .begin() ,a .begin() +1,a.begin(),toupper);
|
Abcdefghi
|
|