The string data type is a primitive data type that represents a sequence of characters.
JavaScript provides both a string primitive and a global String object. The String object essentially wraps the primitive string with methods and properties used to access and manipulate individual characters or the entire string.
String objects can be created by calling the new String() constructor or the String() global function:
var string1 = new String([string]);
var string1 = String([string]);
String primitives can be created using the string literal form:
var string2 = 'string';
var string3 = "string";
Conversion between String objects and string primitives is done automatically, thus the methods of the String object can be called on string primitives as well.
The valueOf method of the String object can be used to manually convert a String object to a string primitive.
Individual characters in a string can be accessed by calling the charAt method with the zero-based index of the desired character:
var name = "Jane Doe"; name.charAt(1); // "a" name.charAt(4); // " "
It's also possible to use the square brackets notation:
name[0]; // "J"
This feature, however, is not supported in all browsers, so it's best to stick with the charAt method.
constructorString objectlengthprototypecharAt(index)charCodeAt(index)concat(string1 [, string2 [, ...]])fromCharCode(num1 [, num2 [, ...]])indexOf(searchString [, offset])lastIndexOf(searchString [, offset])match(pattern)pattern against the input stringreplace(pattern, replacement)search(pattern)slice(offset [, length])split(pattern [, limit])substr(offset [, length])substring(indexStart , [indexStop])toLowerCase()toString()String objecttoUpperCase()valueOf()If you see a typo, want to make a suggestion or have anything in particular you'd like to know more about, please drop us an e-mail at hello at diveintojavascript dot com.
Copyright © 2010-2013 Dive Into JavaScript