Programming
Bash
Arrays
Regex - grep
Strings
C
Arrays
Regex - POSIX
Strings
C++
Arrays
Bimaps
Functions
Maps
Regex
Strings
Go
Arrays
Maps
Strings
Java
Arrays
Regex
Strings
Create / delete
Initialise
Count
Retrieve by index
Slice
Compare
Search
Split
Insert
Overwrite
Replace
Remove by value
Remove by index
Trim
Sort
Convert case
JavaScript
Arrays
Functions
Objects
Regex
Strings
Perl
Arrays
Regex
Strings
Python
Arrays
Functions
Regex
Strings
Java Strings
Create / delete
Create string String a = "";
a = new String("abcdef");
abcdef
Initialise
Fixed contents a = "";
a = "abcdef";
abcdef
Repeated value 'x' x 10 a = "";
ca = new char [10]; Arrays.fill(ca,'x'); a=String.valueOf(ca);
xxxxxxxxxx
Count
Length of string a="abc";
i=a.length();
3
Number of 'X' chars in string a="abcXdefXX";
n=a.replaceAll("[^X]","").length();
3
Number of 'X', 'Y' and 'Z' chars in string a="abcXdefYZ";
n=a.replaceAll("[^XYZ]","").length();
3
Number of 'abc' substrings in string a="abcabcdefabc";
n=a.split("abc").length;
3
Is string empty? a="";
t=a.isEmpty();
1
Retrieve by index
Retrieve 1st char a="abc";
c=a.charAt(0);
a
Retrieve last char a="abc";
c=a.charAt(a.length()-1);
c
Retrieve char 'n' a="abcdef";
n=2;
c=a.charAt(n);
c
Retrieve out-of-bounds char a="abc";
n=4;
b="";
try { c=a.charAt(n); }
catch (java.lang.StringIndexOutOfBoundsException e) { b="out of range"; }
out of range
Slice
Slice from index 'm' to 'n' (inclusive) a="abcdefghi";
m=3;
n=6;
s=a.substring(m,n+1);
defg
Slice from index 'n' (inclusive) a="abcdefghi";
n=3;
s=a.substring(n);
defghi
Slice up to index 'n' (inclusive) a="abcdefghi";
n=3;
s=a.substring(0,n+1);
abcd
Compare
Test for equality (case sensitive) a="abcdef";
b="abc";
t=a.equals(b);
0
Test for equality (case insensitive) a="abcdef";
b="ABCdef";
t=a.equalsIgnoreCase(b);
1
Test for prefix a="abcdef";
b="abc";
t=a.startsWith(b);
1
Test for suffix a="abcdef";
b="def";
t=a.endsWith(b);
1
Search
Test char 'is in' string a="abcdefghi";
c='c';
t=(a.indexOf(c)!=-1);
1
Test substring 'is in' string a="abcdefghi";
b="abc";
t=(a.indexOf(b)!=-1);
1
Test substring 'is in' string a="abcdefghi";
b="abc";
t=a.contains(b);
1
Find first occurrence of char a="abcdefccccc";
c='c';
i=a.indexOf(c);
2
Find last occurrence of char a="abcdefccccc";
c='c';
i=a.lastIndexOf(c);
10
Find first occurrence of string a="abcdefabc";
b="abc";
i=a.indexOf(b);
0
Find last occurrence of string a="abcdefabc";
b="abc";
i=a.lastIndexOf(b);
6
Split
Split on char a="one,two,three";
b=",";
sa=a.split(b);
one two three
Split on string a="aa_x_bb_x_cc";
b="_x_";
sa=a.split(b);
aa bb cc
Split on char outside quotes a="one,\"two,three\",four";
b=",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
sa=a.split(b);
one "two,three" four
Iterate
Iterate through string a="abcdef";
b="";
for (i=0; i<a.length(); i++)
{ b=b+a.charAt(i); b=b+","; }
a,b,c,d,e,f,
Insert
Prepend char a="bcdef";
c='a';
a=c+a;
abcdef
Prepend char [alternative] a="bcdef";
sb = new StringBuffer(a); sb.insert(0,'a'); a=sb.toString();
abcdef
Append char a="abc";
c='d';
a=a+c;
abcd
Append char [alternative] a="abc";
c='d';
sb=new StringBuffer(a); sb.append(c); a=sb.toString();
abcd
Insert char at index 'n' a="abc";
n=1;
c='X';
a=a.substring(0,n)+c+a.substring(n);
aXbc
Insert char at index 'n' [alternative] a="abc";
n=1;
c='X';
sb=new StringBuffer(a); sb.insert(n,c); a=sb.toString();
aXbc
Prepend string a="def";
b="abc";
a=b+a;
abcdef
Append string a="abc";
b="def";
a+=b;
abcdef
Insert string at index 'n' a="abcghi";
b="def";
n=3;
a=a.substring(0,n) + b + a.substring(n);
abcdefghi
Insert string at index 'n' [alternative] a="abcghi";
b="def";
n=3;
sb=new StringBuffer(a); sb.insert(n,b); a=sb.toString();
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=a.substring(0,n)+c+a.substring(n+1);
aBc
Overwrite char at index 'n' [alternative] a="abc";
n=1;
c='B';
sb=new StringBuffer(a); sb.setCharAt(n,c); a=sb.toString();
aBc
Replace
Replace 1st instance of char 'X' with 'Y' a="XXXYYYZZZ";
a=a.replaceFirst("X","Y");
YXXYYYZZZ
Replace all instances of char 'X' with 'Y' a="XXXYYYZZZ";
a=a.replace('X','Y');
YYYYYYZZZ
Replace 1st instance of substring 'abc' with 'XYZ' a="abcdefabc";
a=a.replaceFirst("abc","XYZ");
XYZdefabc
Replace all instances of substring 'abc' with 'XYZ' a="abcdefabc";
a=a.replaceAll("abc","XYZ");
XYZdefXYZ
Replace all instances of 'X','Y','Z' with 'A','B','C' respectively a="XXXYYYZZZ";
a=a.replace('X','A').replace('Y','B').replace('Z','C');
AAABBBCCC
Remove by value
Remove 1st instance of char 'X' a="XXXYYYZZZ";
a=a.replaceFirst("X","");
XXYYYZZZ
Remove all instances of char 'X' a="XXXYYYZZZ";
a=a.replaceAll("X","");
YYYZZZ
Remove 1st instance of substring 'abc' a="abcdefabc";
a=a.replaceFirst("abc","");
defabc
Remove all instances of substring 'abc' a="abcdefabc";
a=a.replaceAll("abc","");
def
Remove prefix 'abc.' a="abc.abc.def";
a=a.replaceFirst("^abc[.]","");
abc.def
Remove suffix '.def' a="abc.def.def";
a=a.replaceFirst("[.]def$","");
abc.def
Remove by index
Remove 1st char a="abcdef";
a=a.substring(1,a.length());
bcdef
Remove last char a="abcdefghi";
a=a.substring(0,a.length()-1);
abcdefgh
Remove char at index 'n' a="abcdef";
n=3;
a=a.substring(0,n)+a.substring(n+1);
abcef
Remove chars in index range 'm' to 'n' (inclusive) a="abcdef";
m=2;
n=4;
a=a.substring(0,m)+a.substring(n+1);
abf
Remove chars from index 'n' onwards (inclusive) a="abcdefghi";
n=3;
a=a.substring(0,n);
abc
Remove chars up to index 'n' (inclusive) a="abcdefghi";
n=3;
a=a.substring(n+1);
efghi
Trim
Trim leading white space a=" abc def ";
a=a.replaceAll("^\\s+", "");
abc def
Trim trailing white space a=" abc def ";
a=a.replaceAll("\\s+$", "");
   abc def
Trim leading and trailing white space a=" abc def ";
a=a.trim();
abc def
Trim trailing carriage return a="abcdef\n";
a=a.replaceAll("\\n$","");
abcdef
Sort
Sort in ascending order a="cadefb";
ca=a.toCharArray(); Arrays.sort(ca); a=String.valueOf(ca);
abcdef
Reverse a="abcdefghi";
a=(new StringBuffer(a)).reverse().toString();
ihgfedcba
Convert case
Convert to lower case a="ABCDEFGHI";
a=a.toLowerCase();
abcdefghi
Convert to upper case a="abcdefghi";
a=a.toUpperCase();
ABCDEFGHI
Convert first char to lower case a="ABCDEFGHI";
a=(Character.toLowerCase(a.charAt(0))+a.substring(1));
aBCDEFGHI
Convert first char to upper case a="abcdefghi";
a=(Character.toUpperCase(a.charAt(0))+a.substring(1));
Abcdefghi