Programming
Bash
Arrays
Regex - grep
Strings
C
Arrays
Regex - POSIX
Strings
C++
Arrays
Bimaps
Functions
Maps
Regex
Strings
Go
Arrays
Maps
Strings
Create / delete
Initialise
Count
Retrieve by index
Slice
Compare
Search
Split
Iterate
Print
Insert
Overwrite
Replace
Remove by value
Remove by index
Trim
Sort
Convert case
Java
Arrays
Regex
Strings
JavaScript
Arrays
Functions
Objects
Regex
Strings
Perl
Arrays
Regex
Strings
Python
Arrays
Functions
Regex
Strings
Go Strings
Create / delete
Create string var a = "";
a = "abcdef";
abcdef
Initialise
Fixed contents var a = "";
a = "abcdef";
abcdef
Repeated value 'x' x 10 var a = ""
a=strings.Repeat("x", 10)
xxxxxxxxxx
Count
Length of string var a="abc"
var i=len(a)
3
Number of 'X' chars in string var a="abcXdefXX"
var n=strings.Count(a,"X")
3
Number of 'abc' substrings in string var a="abcabcdefabc";
var n=strings.Count(a,"abc")
3
Retrieve by index
Retrieve 1st char var a="abc"
var c=a[0]
a
Retrieve last char var a="abc"
var c=a[len(a)-1]
c
Retrieve char 'n' var a="abcdef"
var n=2
var c=a[n]
c
Slice
Slice from index 'm' to 'n' (inclusive) var a="abcdefghi"
var m=3
var n=6
var s=a[m:n+1]
defg
Slice from index 'n' (inclusive) var a="abcdefghi"
var n=3
var s=a[n:]
defghi
Slice up to index 'n' (inclusive) var a="abcdefghi"
var n=3
var s=a[:n+1]
abcd
Compare
Test for equality (case sensitive) var a="abcdef"
var b="abc"
var t=(a==b)
0
Test for equality (case insensitive) var a="abcdef"
var b="ABCdef"
var t=strings.EqualFold(a, b)
1
Test for prefix var a="abcdef"
var b="abc"
var t=strings.HasPrefix(a,b)
1
Test for suffix var a="abcdef"
var b="def"
var t=strings.HasSuffix(a,b)
1
Search
Test char 'is in' string var a="abcdefghi"
var c='c'
var t=strings.Contains(a, string(c))
1
Test substring 'is in' string var a="abcdefghi"
var b="abc"
var t=strings.Contains(a, b)
1
Find first occurrence of char var a="abcdefccccc"
var c='c'
var i=strings.Index(a, string(c))
2
Find last occurrence of char var a="abcdefccccc"
var c='c'
var i=strings.LastIndex(a, string(c))
10
Find first occurrence of string var a="abcdefabc"
var b="abc"
var i=strings.Index(a, b)
0
Find last occurrence of string var a="abcdefabc"
var b="abc"
var i=strings.LastIndex(a, b)
6
Find first occurrence of any char in set var a="abcdef"
var b="xdc"
var i=strings.IndexAny(a, b)
2
Find last occurrence of any char in set var a="abcdef"
var b="xdc"
var i=strings.LastIndexAny(a, b)
3
Find first occurrence of any char not in set var a="abcdef";
var b="abcx";
f := func(c rune) bool {
  return ! strings.Contains(b, string(c));
}
var i=strings.IndexFunc(a, f)
3
Find last occurrence of any char not in set var a="abcdef";
var b="xdef";
f := func(c rune) bool {
  return ! strings.Contains(b, string(c));
}
var i=strings.LastIndexFunc(a, f)
2
Split
Split on char var a="one,two,three"
var c=','
var sa=strings.Split(a, string(c))
one two three
Split on string var a="aa_x_bb_x_cc"
var b="_x_"
var sa=strings.Split(a, b)
aa bb cc
Split limited to 'n' parts var a="one,two,three"
var c=','
var n=2
var sa=strings.SplitN(a, string(c), n)
one two,three
Iterate
Iterate through string var a="abcdef"
var b=""
// _, x because we don't need index
for _, x := range a {
  b=b+string(x)+","
}
a,b,c,d,e,f,
Print
Basic printing var a="abcdef"
fmt.Println(a);
abcdef
Formatting sprintf style var a="abc"
var b="abcdef"
var s=fmt.Sprintf("%10s %10s",a, b)
       abc abcdef
Insert
Prepend char var a="bcdef"
var c='a'
a=string(c)+a
abcdef
Append char var a="abc"
var c='d'
a+=string(c);
abcd
Insert char at index 'n' var a="abc"
var c='X'
var n=1
a=a[:n]+string(c)+a[n:]
aXbc
Prepend string var a="def"
var b="abc"
a=b+a
abcdef
Append string var a="abc"
var b="def"
a+=b;
abcdef
Insert string at index 'n' var a="abcghi"
var b="def"
var n=3
a=a[:n]+b+a[n:]
abcdefghi
Concatenate 'a' and 'b' var a="abc"
var b="def"
var s=a+b
abcdef
Overwrite
Overwrite char at index 'n' var a="abc"
var n=1
var c='B'
// string immutable so convert to byte array
// this is most probably not safe for >ASCII
var aa=[]byte(a)
aa[n]=byte(c)
a=string(aa)
aBc
Replace
Replace 1st instance of char 'X' with 'Y' var a="XXXYYYZZZ";
a=strings.Replace(a, "X", "Y", 1)
YXXYYYZZZ
Replace all instances of char 'X' with 'Y' var a="XXXYYYZZZ";
a=strings.Replace(a, "X", "Y", -1)
YYYYYYZZZ
Replace 1st instance of substring 'abc' with 'XYZ' var a="abcdefabc";
a=strings.Replace(a, "abc", "XYZ", 1)
XYZdefabc
Replace all instances of substring 'abc' with 'XYZ' var a="abcdefabc";
a=strings.Replace(a, "abc", "XYZ", -1)
XYZdefXYZ
Remove by value
Remove 1st instance of char 'X' var a="XXXYYYZZZ";
a=strings.Replace(a, "X", "", 1)
XXYYYZZZ
Remove all instances of char 'X' var a="XXXYYYZZZ";
a=strings.Replace(a, "X", "", -1)
YYYZZZ
Remove 1st instance of substring 'abc' var a="abcdefabc";
a=strings.Replace(a, "abc", "", 1)
defabc
Remove all instances of substring 'abc' var a="abcdefabc";
a=strings.Replace(a, "abc", "", -1)
def
Remove prefix 'abc.' var a="abc.abc.def"
a=strings.TrimPrefix(a, "abc.")
abc.def
Remove suffix '.def' var a="abc.def.def"
a=strings.TrimSuffix(a, ".def")
abc.def
Remove by index
Remove 1st char var a="abcdef"
a=a[1:]
bcdef
Remove last char var a="abcdefghi"
a=a[:len(a)-1]
abcdefgh
Remove char at index 'n' var a="abcdef"
var n=3
a=a[:n]+a[n+1:]
abcef
Remove chars in index range 'm' to 'n' (inclusive) var a="abcdef"
var m=2
var n=4
a=a[:m]+a[n+1:]
abf
Remove chars from index 'n' onwards (inclusive) var a="abcdefghi"
var n=3
a=a[:n]
abc
Remove chars up to index 'n' (inclusive) var a="abcdefghi"
var n=3
a=a[n+1:]
efghi
Trim
Trim leading white space var a=" abc def "
a=strings.TrimLeft(a, " ")
abc def
Trim trailing white space var a=" abc def "
a=strings.TrimRight(a, " ")
   abc def
Trim leading and trailing white space var a=" abc def "
a=strings.Trim(a, " ")
abc def
Trim trailing carriage return var a="abcdef\n"
a=strings.Trim(a, "\n")
abcdef
Sort
Sort in ascending order var a="cafdeb"
s := strings.Split(a, "")
sort.Strings(s)
a=strings.Join(s, "")
abcdef
Sort in descending order var a="cafdeb"
s := strings.Split(a, "")
sort.Sort(sort.Reverse(sort.StringSlice(s)))
a=strings.Join(s, "")
fedcba
Sort in custom order // The sort.Interface methods:
// Len, Less and Swap
type InReverse []string
func (s InReverse) Len() int {
  return len(s)
}
func (s InReverse) Less(i, j int) bool {
  return s[i] > s[j]
}
func (s InReverse) Swap(i, j int) {
  s[i], s[j] = s[j], s[i]
}
var a="cafdeb";
s := strings.Split(a, "")
sort.Sort(InReverse(s))
a=strings.Join(s, "")
fedcba
Convert case
Convert to lower case var a="ABCDEFGHI"
a=strings.ToLower(a)
abcdefghi
Convert to upper case var a="abcdefghi"
a=strings.ToUpper(a)
ABCDEFGHI