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. If you're feeling geeky, you can take a look at the demo file, BuiltInObjects/Demos/stringsAndStrings.html, which compares primitive type strings to String objects.

Some common string properties and methods are shown below. In all the examples, the variable webucator is the string "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. Note that it is zero-based, so the first letter in the string is at position 0.
webucator.charAt(4)
//Returns c (the fifth character)

charCodeAt(position) Returns the Unicode character code of the character at the specified position.
webucator.charCodeAt(4)
//Returns 99

fromCharCode(characterCodes) Returns the text representation of the specified comma-delimited character codes. Used with String rather than a specific String object.
String.fromCharCode(169)
//Returns ©

indexOf(substring,startPosition) Searches from startPosition for substring. Returns the position at which the substring is found. If substring is not found, returns -1.
webucator.indexOf("cat");
//Returns 4

webucator.indexOf("cat", 5);
//Returns -1

lastIndexOf(substring,endPosition) Searches from the end of the string for substring until endPosition is reached. Returns the position at which the substring is found. If substring is not found, returns -1.
webucator.lastIndexOf("cat");
//Returns 4

webucator.lastIndexOf("cat", 5);
//Returns 4

substring(startPosition,endPosition) Returns the substring beginning at startPosition and ending with the character before endPosition. endPosition 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(startPosition,length) Returns the substring of Length characters beginning at startPosition. length 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(startPosition,endPosition) Same as substring(startPosition, endPosition).
webucator.slice(4, 7);
//Returns cat

slice(startPosition,positionFromEnd) positionFromEnd is a negative integer. Returns the substring beginning at startPosition and ending positionFromEnd 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. In the next activity, we'll show you how the String.split() method works.

Next