Programming
Bash
Arrays
Regex - grep
Strings
C
Arrays
Regex - POSIX
Strings
C++
Arrays
Bimaps
Functions
Maps
Regex
Strings
Create / delete
Initialise
Count
Retrieve by index
Slice
Compare
Search
Split
Print
Insert
Overwrite
Replace
Remove by value
Remove by index
Trim
Sort
Convert case
Recipes
Go
Arrays
Maps
Strings
Java
Arrays
Regex
Strings
JavaScript
Arrays
Functions
Objects
Regex
Strings
Perl
Arrays
Regex
Strings
Python
Arrays
Functions
Regex
Strings
C++ Strings
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.capacity();
6
Maximum string size a="abc";
st=a.max_size();
4611686018427387897
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 struct fn_xyz
{
  bool operator()(char c)
  {
    return
    (
      c >= 'X' &&
      c <= 'Z'
    );
  }
};
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 [alternative] a="abc";
c=*(a.begin());
a
Retrieve last char a="abc";
c=a[a.length()-1];
c
Retrieve last char [alternative] a="abc";
c=*(a.end()-1);
c
Retrieve char 'n' a="abcdef";
n=2;
c=a[n];
c
Retrieve out-of-bounds char a="abc";
n=4;
b="";
try
{
  c=a.at(n);
}
catch(std::out_of_range& e)
{
  b="out of range";
}
out of range
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 first occurrence of char a="abcdefccccc";
c='c';
i=a.find(c);
2
Find last occurrence of char a="abcdefccccc";
c='c';
i=a.rfind(c);
10
Find first occurrence of string a="abcdefabc";
b="abc";
i=a.find(b);
0
Find last occurrence of string a="abcdefabc";
b="abc";
i=a.rfind(b);
6
Find first occurrence of any char in set a="abcdef";
b="xdc";
i=a.find_first_of(b);
2
Find last occurrence of any char in set a="abcdef";
b="xdc";
i=a.find_last_of(b);
3
Find first occurrence of any char not in set a="abcdef";
b="abcx";
i=a.find_first_not_of(b);
3
Find last occurrence of any char not in set a="abcdef";
b="xdef";
i=a.find_last_not_of(b);
2
Split
Split on char a="one,two,three";
c=',';
sa.clear();
stringstream ss(a);
string segment;
// default separator is newline
while(getline(ss, segment, ','))
{
  sa.push_back(segment);
}
one two three
Split on char [alternative] a="one,two,three";
c=',';
sa.clear();
while (a.length())
{
  sa.push_back(a.substr(0, a.find(c)));
  a.erase(0,a.find(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(b)));
  a.erase(0,a.find(b));
  a.erase(0,a.find(b)+b.length());
}
aa bb cc
Split on any char in set a="one,two;three";
b=",;";
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));
}
one two three
Print
Basic printing a="abcdef";
cout << a << endl;
abcdef
Insert
Prepend char a="bcdef";
c='a';
a=c+a;
abcdef
Prepend char [alternative] a="bcdef";
c='a';
a.insert(a.begin(),c);
abcdef
Append char a="abc";
c='d';
a+=c;
abcd
Append char [alternative] 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 [alternative] a="def";
b="abc";
a.insert(0,b);
abcdef
Append string a="abc";
b="def";
a+=b;
abcdef
Append string [alternative] 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 by value
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";
itt = remove(a.begin(),a.end(),'X');
a.erase(itt,a.end());
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 prefix 'abc.' a="abc.abc.def";
if (a.find("abc.")==0)
{
  a.erase(0,strlen("abc."));
}
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
Recipes
Swap last two components of 'a' delimited by 'c' a="DAD02-44-1A-D1";
c='-';
size_t l = a.rfind(c);
size_t p = a.rfind(c,l-1);
b = a.substr(0,p);
b += a.substr(l);
b += a.substr(p,l-p);
DAD02-44-D1-1A