String

Contact Us or call 1-877-932-8228
String

String

In JavaScript, there are two types of string data types: primitive strings and String objects. String objects have many methods for manipulating and parsing strings of text. Because these methods are available to primitive strings as well, in practice, there is no need to differentiate between the two types of strings.

Some common string properties and methods are shown below. In all the examples, the variable webucator contains "Webucator".

Common String Properties
Property Description
length Read-only value containing the number of characters in the string.
webucator.length; //Returns 9
Common String Methods
Method Description
charAt(position) Returns the character at the specified position.
webucator.charAt(4); //Returns c
charCodeAt(position) Returns the Unicode character code of the character at the specified position.
webucator.charCodeAt(4); //Returns 99
fromCharCode(charCodes) Returns the text representation of the specified comma-delimited character code. Used with String rather than a specific String object.
String.fromCharCode(169); //Returns 
indexOf(substr,startPos) Searches from startPos (or the beginning of the string, if startPos is not supplied) for substr. Returns the first position at which substr is found or -1 if substr is not found.
webucator.indexOf("cat"); //Returns 4

webucator.indexOf("cat", 5); //Returns -1
lastIndexOf(substr,endPos) Searches from endPos (or the end of the string, if endPos is not supplied) for substr. Returns the last position at which substr is found or -1 is substr is not found.
webucator.lastIndexOf("cat"); //Returns 4

webucator.lastIndexOf("cat", 5); //Returns 4
substring(startPos,endPos) Returns the substring beginning at startPos and ending with the character before endPos. endPos is optional. If it is excluded, the substring continues to the end of the string.
webucator.substring(4, 7); //Returns cat

webucator.substring(4); //Returns cator
substr(startPos,len) Returns the substring of Length characters beginning at startPos. len is optional. If it is excluded, the substring continues to the end of the string.
webucator.substr(4, 3); //Returns cat

webucator.substr(4); //Returns cator
slice(startPos,endPos) Same as substring(startPos,endPos).
webucator.slice(4, 7); //Returns cat
slice(startPos,posFromEnd) posFromEnd is a negative integer. Returns the substring beginning at startPos and ending posFromEnd characters from the end of the string.
webucator.slice(4, -2); //Returns cat
split(delimiter) Returns an array by splitting a string on the specified delimiter.
var s = "A,B,C,D"; 
var a = s.split(",");
document.write(a[2]); //Returns C
toLowerCase() Returns the string in all lowercase letters.
webucator.toLowerCase() //Returns webucator
toUpperCase() Returns the string in all uppercase letters.
webucator.toUpperCase(); //Returns WEBUCATOR

You can see these examples in a browser by opening BuiltInObjects/Demos/StringPropertiesAndMethods.html.

Next