JS String Functions

String can be initialized by new keyword or double quotation marks " :

var s = new String("endmemo.com");
var s = "endmemo.com";

length property: get the string length:

var s = "endmemo.com";
var len = s.length; //s=11

String characters are indexed from 0 to String.length - 1, and can be access by using brackets []:

var s = "endmemo.com";
var x = s[2]; x = 'd'

charAt() method: get the character at the specified position:

var s = "endmemo.com";
var c = s.charAt(3); //c = 'm'

charCodeAt() method: get the character unicode at the specified position:

var s = "endmemo.com";
var c = s.charCodeAt(3); //c = 109

indexOf() method: Search string and return the first occurrence position, if not found, return -1:

var s = new String("endmemo.com");
var p = s.indexOf('m') //p = 3
p = s.indexOf("g") //p = -1
p = s.indexOf("mo") //p = 5

lastIndexOf() method: Search string from the end, and return the position of the first found occurrence, if not found, return -1:

var s = new String("endmemo.com");
var p = s.lastIndexOf('m') //p = 10
p = s.lastIndexOf("g") //p = -1
p = s.lastIndexOf("e") //p = 4

slice(s,e) method: cut string, s - start position, e - end position (optional), if not provided, it's the end of the string:

var s = "endmemo.com";
var s2 = arr.slice(2,5); //s2 is "dme"
var s2 = arr.slice(2); //s2 is "dmemo.com"

valueOf() method: returns the primitive string:

var s = "endmemo.com";
var x = s.valueOf(); //x = "endmemo.com"

concat() method: join strings:

var s1 = "endmemo.com";
var s2 = "javascript";
var s3 = "tutorial";
var s = s1.concat(" ", s2, " ", s3);
//s is "endmemo.com javascript tutorial"

fromCharCode() static method: convert unicode to character:

var c = String.fromCharCode(109); // c = 'm'

match() method: regular expression pattern match, return matches:

var s = "endmemo.com";
var m = s.match(/.m/g); //m is an array returned
m.valueOf(); //dm,em,om

replace() method: replace a substring with new, supports regular expression:

var s = "endmemo.com R language tutorial";
var x = s.replace("R language","javascript");
//x is "endmemo.com javascript tutorial"
var x = s.replace(/\sR.+ge\s/,"javascript");
//same result, using regular expression

search() method: return the occurrence position of a substring or regular expression pattern:

var s = "endmemo.com";
var p = s.search("dm"); //p is 2
var p = s.search(/e.o/); //p is 4
var p = s.search(/\s\d/); //p is -1, not found

split() method: split a string into an array:

var s = "endmemo.com";
var arr = s.split("m"); //arr is ["end","e","o.co"]
var arr = s.split("me"); //arr is ["end","mo.com"]

Click here to see how we read a textarea and split the content into an array of lines by "\n" in function calcs().

substr() method: cut string by a specified position and cut length:

var s = "endmemo.com";
var x = s.substr(2,2); //x="dm"
var x = s.substr(2); //x="dmemo.com"

substring() method: cut string by two positions:

var s = "endmemo.com";
var x = s.substring(2,4); //x="dm"
var x = s.substring(2); //x="dmemo.com"

toLowerCase() method: convert to lower case,
toUpperCase() method: convert to upper case:

var s = "endmemo.com";
var x = s.toUpperCase(); //x is "ENDMEMO.COM"
var y = x.toLowerCase(); //y is "endmemo.com"

constructor property: returns the string constructing prototype:

var s = "endmemo.com";
var x = s.constructor;
//x = "function String() {[native code]}"

prototype property: add property or method to String. Following example upper case all first character of the string words:

String.prototype.UpperFirst = function()
	var res = "";
	for (var i=0; i<this.length;i++)
		if (i == 0 || this.charCodeAt(i-1) == 32)
		res += this[i].toUpperCase();
	return res;
var s = "end memo com";
var x = s.UpperFirst(); //x is "End Memo Com"

endmemo.com © 2020  Terms of Use | Home