Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla
Your Search Results

    Text formatting

    This article is in need of a technical review.

    String Object

    The String object is a wrapper around the string primitive data type. Do not confuse a string literal with the String object. For example, the following code creates the string literal s1 and also the String object s2:

    var s1 = "foo"; //creates a string literal value
    var s2 = new String("foo"); //creates a String object
    

    You can call any of the methods of the String object on a string literal value—JavaScript automatically converts the string literal to a temporary String object, calls the method, then discards the temporary String object. You can also use the String.length property with a string literal.

    You should use string literals unless you specifically need to use a String object, because String objects can have counterintuitive behavior. For example:

    var s1 = "2 + 2"; //creates a string literal value
    var s2 = new String("2 + 2"); //creates a String object
    eval(s1); //returns the number 4
    eval(s2); //returns the string "2 + 2"

    A String object has one property, length, that indicates the number of characters in the string. For example, the following code assigns x the value 13, because "Hello, World!" has 13 characters:

    var mystring = "Hello, World!";
    var x = mystring.length;
    

    A String object has two types of methods: those that return a variation on the string itself, such as substring and toUpperCase, and those that return an HTML-formatted version of the string, such as bold and link.

    For example, using the previous example, both mystring.toUpperCase() and "hello, world!".toUpperCase() return the string "HELLO, WORLD!"

    The substring method takes two arguments and returns a subset of the string between the two arguments. Using the previous example, mystring.substring(4, 9) returns the string "o, Wo". See the substring method of the String object in the JavaScript Reference for more information.

    The String object also has a number of methods for automatic HTML formatting, such as bold to create boldface text and link to create a hyperlink. For example, you could create a hyperlink to a hypothetical URL with the link method as follows:

    mystring.link("http://www.helloworld.com")
    

    The following table summarizes the methods of String objects.

    Table 7.4 Methods of String Instances
    Method Description
    anchor Creates HTML named anchor.
    big, blink, bold, fixed, italics, small, strike, sub, sup Create HTML formatted string.
    charAt, charCodeAt Return the character or character code at the specified position in string.
    indexOf, lastIndexOf Return the position of specified substring in the string or last position of specified substring, respectively.
    link Creates HTML hyperlink.
    concat Combines the text of two strings and returns a new string.
    fromCharCode Constructs a string from the specified sequence of Unicode values. This is a method of the String class, not a String instance.
    split Splits a String object into an array of strings by separating the string into substrings.
    slice Extracts a section of an string and returns a new string.
    substring, substr Return the specified subset of the string, either by specifying the start and end indexes or the start index and a length.
    match, replace, search Work with regular expressions.
    toLowerCase, toUpperCase

    Return the string in all lowercase or all uppercase, respectively.

    Document Tags and Contributors

    Tags: 
    Contributors to this page: kscarfone, fscholz
    Last updated by: kscarfone,