Javascript String Functions


LENGTH

var str = 'Demo text.';
var len = str.length;
var len2 = 'my other text'.length;


CHARAT

var str = 'Demo Text.';
var c = str.charAt(5);

for (i=0; i < str.length; i++){
alert(str.charAt(i));
}


TOLOWERCASE

var str = 'Demo Text.';
var strL = str.toLowerCase();
var strU = str.toUpperCase();

if (str.toLowerCase() == 'demo text.')


TOUPPERCASE

var str = 'Demo Text.';
var strL = str.toLowerCase();
var strU = str.toUpperCase();

if (str.toUpperCase() == 'demo text.')


INDEXOF

Returns -1 or index of where search string appears in string. Optionally specify where in the string to start searching.

var str = 'Demo Text.';
var pos = str.indexOf('Te');
var pos2 = str.indexOf('Te',10);


SPLIT

Split sentance into an array of words

var str = 'Demo Text.';
var words = new Array();

words = str.split(' ');


SUBSTR

Specify start position of cut and length of cut.

var str = 'Demo Text.';
var str1 = str.substr(3,4);


SUBSTRING

Specify start position of cut and end position of cut.

var str = 'Demo Text.';
var str1 = str.substring(3,4);