String properties and methods

1. Properties:

To determine the length of a string, use the string's length property. For example, to get the length of the string s:

    s.length

2. Method:

In addition to the length property, strings also provide many methods to call:

    var s = "hello, world";

    s.charAt(0); //"h": the first character. Equivalent to s[0]

    s.charAt(s.length-1); //"d": the last character. Equivalent to s[s.length-1]

    s.substring(1,4); //"ell": 2~4 characters

    s.slice(1,4); //"ell": same as above

    s.slice(-4); //"orld": the last four characters

    s.indexOf("l"); //2: The position where the character l first appears

    s.lastIndexOf("l"); //10: The position of the last occurrence of the character l

    s.indexOf("l",3); //3: The position where the character l appears for the first time in position 3 and later

    s.splite(", "); //["hello","world"] split into substrings

    s.replace("h","H"); //"Hello, world": full text string replacement

    s.toUppercase() //"HELLO, WORLD"

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326633005&siteId=291194637