String - slice() v substring()

  • char at <end> will not be displayed
  • <end> is optional
slice(start, end)substring(start, end)
start == endreturns empty stringreturns empty string
end is omittedextracts characters to the end of the stringreturns empty string
start / end > string.lengthstring's length will be used insteadreturns empty string
start > endreturns empty stringswaps two arguments
start = -vesets char from the end of stringtreated as 0
end = -veMath.max(0, string.length + end)treated as 0
start / end = NaNtreated as 0treated as 0

String - substr()

substr(start, length)
length - omitted / > string.lengthextracts characters to the end of the string
start > string.lengthreturns empty string
start / length = -vereturns empty string
start = NaNtreated as 0
length = NaNreturns empty string

Examples

string = "0123456789"

string char"0""1""2""3""4""5""6""7""8""9"
index from left0123456789
index from right-10-9-8-7-6-5-4-3-2-1
startend / lengthslice(start, end)substring(start, end)substr(start, length)
131212123
733456789
33345
101123456789
2100234567892345678923456789
100101
-7-33456
-3-7
-7312345
-37123456789
7-3123456
3-712
3345678934567893456789
-3789123456789789
NaN8123456712345671234567
NaN12123456789123456789123456789
NaN-712
NaN-10
3NaN12
-3NaN
NaNNaN