Cynical news

Links
Literature
Music
Programming
Software
Programming
JavaScript Strings
JavaScript Strings
Count

Length of string a="abc";
i=a.length;
3

Retrieve by index

Retrieve 1st char a="abc";
c=a[0];
a
Retrieve last char a="abc";
c=a[a.length-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.slice(m,n+1);
defg
Slice from index 'n' (inclusive) a="abcdefghi";
n=3;
s=a.slice(n);
defghi
Slice up to index 'n' (inclusive) a="abcdefghi";
n=3;
s=a.slice(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";
re=new RegExp('^'+b);
t=a.match(re);
1
Test for suffix a="abcdef";
b="def";
re=new RegExp(b+'$');
t=a.match(re);
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
Find index of char (forward) a="abcdefccccc";
c='c';
i=a.indexOf(c);
2
Find index of char (reverse) a="abcdefccccc";
c='c';
i=a.lastIndexOf(c);
10
Find index of substring (forward) a="abcdefabc";
b="abc";
i=a.indexOf(b);
0
Find index of substring (reverse) a="abcdefabc";
b="abc";
i=a.lastIndexOf(b);
6

Split

Split on char a="one,two,three";
c=',';
sa=a.split(c);
one two three

Evaluate

Evaluate empty string a="";
t=a;
0
Evaluate non-empty string a="abcdef";
t=a;
1

Insert

Prepend char a="bcdef";
c='a';
a=c+a;
abcdef
Append char a="abc";
c='d';
a=a+c;
abcd
Prepend string a="def";
b="abc";
a=b+a;
abcdef
Append string a="abc";
b="def";
a=a+b;
abcdef
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 .slice(0 ,n)+c+a.slice(n+1);
aBc
Overwrite substring at index 'n' a="abcdefghi";
b="DEF";
n=3;
a=a .slice(0 ,n) +b+a.slice(n+b.length);
abcDEFghi

Replace

Replace 1st instance of char 'X' with 'Y' a="XXXYYYZZZ"
a=a.replace('X','Y');
YXXYYYZZZ
Replace all instances of char 'X' with 'Y' a="XXXYYYZZZ"
a=a.replace(/X/g,'Y');
YYYYYYZZZ
Replace 1st instance of substring 'abc' with 'XYZ' a="abcdefabc"
a=a .replace('abc','XYZ');
XYZdefabc
Replace all instances of substring 'abc' with 'XYZ' a="abcdefabc"
a=a .replace(/abc/g,'XYZ');
XYZdefXYZ
Replace all instances of 'X','Y','Z' with 'A','B','C' respectively a="XXXYYYZZZ";
a=a .replace(/X/g ,'A') .replace(/Y/g ,'B').replace(/Z/g,'C');
AAABBBCCC

Remove

Remove 1st instance of char 'X' a="XXXYYYZZZ";
a=a.replace('X','');
XXYYYZZZ
Remove all instances of char 'X' a="XXXYYYZZZ";
a=a.replace(/X/g,'');
YYYZZZ
Remove 1st instance of substring 'abc' a="abcdefabc";
a=a.replace('abc','');
defabc
Remove all instances of substring 'abc' a="abcdefabc";
a=a.replace(/abc/g,'');
def
Remove prefix 'abc.' a="abc.abc.def"
a=a.replace(/^abc./,'');
abc.def
Remove suffix '.def' a="abc.def.def"
a=a.replace(/.def$/,'');
abc.def

Remove by index

Remove 1st char a="abcdef";
a=a.slice(1);
bcdef
Remove last char a="abcdefghi";
a=a.slice(0,a.length-1);
abcdefgh
Remove char at index 'n' a="abcdef";
n=3;
a=a .slice(0 ,n)+a.slice(n+1);
abcef
Remove chars in index range 'm' to 'n' (inclusive) a="abcdef";
m=2;
n=4;
a=a .slice(0 ,m)+a.slice(n+1);
abf
Remove chars from index 'n' onwards (inclusive) a="abcdefghi";
n=3;
a=a.slice(0,n);
abc
Remove chars up to index 'n' (inclusive) a="abcdefghi";
n=3;
a=a.slice(n+1);
efghi

Trim

Trim leading white space a=" abc def ";
a=a.replace(/^[ ]*/,'');
abc def
Trim trailing white space a=" abc def ";
a=a.replace(/[ ]*$/,'');
abc def
Trim leading and trailing white space a=" abc def ";
a=a.replace(/^[ ]*/,'').replace(/[ ]*$/,'');
abc def

Sort

Sort in ascending order a="cafdeb";
sa=a.split('');
sa.sort();
a=sa.join('');
abcdef
Sort in descending order a="cafdeb";
sa=a.split('');
sa.sort();
sa.reverse();
a=sa.join('');
fedcba
Sort in custom order a="cafdeb";
sa=a.split('');
sa.sort( function (a, b) { return b.charCodeAt(0) - a.charCodeAt(0); } );
a=sa.join('');
fedcba
Reverse a="abcdefghi";
sa=a.split('');
sa.reverse();
a=sa.join('');
ihgfedcba

Convert case

Convert to lower case a="ABCDEFGHI";
a=a.toLowerCase();
abcdefghi
Convert to upper case a="abcdefghi";
a=a.toUpperCase();
ABCDEFGHI

www.cynicalsoftware.com
14-11-09@15:37:24