美文网首页
string 操作总结

string 操作总结

作者: stone_yao | 来源:发表于2017-01-12 13:41 被阅读23次

    1.String.prototype.slice The slice() method extracts a section of a string and returns a new string.

    var str1 = 'The morning is upon us.', // the length of str1 is 23.
        str2 = str1.slice(1, 8),
        str3 = str1.slice(4, -2),
        str4 = str1.slice(12),
        str5 = str1.slice(30);
    console.log(str2); // OUTPUT: he morn
    console.log(str3); // OUTPUT: morning is upon u
    console.log(str4); // OUTPUT: is upon us.
    console.log(str5); // OUTPUT: ""
    

    2.String.prototype.split() The split() method splits a String object into an array of strings by separating the string into substrings.

    var myString = 'Hello World. How are you doing?';
    var splits = myString.split(' ', 3);
    
    console.log(splits);
    //["Hello", "World.", "How"]
    

    3.String.prototype.indexOf() The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.

    'Blue Whale'.indexOf('Blue');     // returns  0
    'Blue Whale'.indexOf('Blute');    // returns -1
    'Blue Whale'.indexOf('Whale', 0); // returns  5
    'Blue Whale'.indexOf('Whale', 5); // returns  5
    'Blue Whale'.indexOf('');         // returns  0
    'Blue Whale'.indexOf('', 9);      // returns  9
    'Blue Whale'.indexOf('', 10);     // returns 10
    'Blue Whale'.indexOf('', 11);     // returns 10
    

    4.String.prototype.lastIndexOf() The lastIndexOf() method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from fromIndex. Returns -1 if the value is not found.

    'canal'.lastIndexOf('a');     // returns 3
    'canal'.lastIndexOf('a', 2);  // returns 1
    'canal'.lastIndexOf('a', 0);  // returns -1
    'canal'.lastIndexOf('x');     // returns -1
    'canal'.lastIndexOf('c', -5); // returns 0
    'canal'.lastIndexOf('c', 0);  // returns 0
    'canal'.lastIndexOf('');      // returns 5
    'canal'.lastIndexOf('', 2);   // returns 2
    

    5.String.prototype.search() The search() method executes a search for a match between a regular expression and this String object.

    //The following example logs a message which depends on the success of the test.
    function testinput(re, str) {
      var midstring;
      if (str.search(re) != -1) {
        midstring = ' contains ';
      } else {
        midstring = ' does not contain ';
      }
      console.log(str + midstring + re);
    }
    

    6.String.prototype.substring() The substring() method returns a subset of a string between one index and another, or through the end of the string.

    var anyString = 'Mozilla';
    
    // Displays 'Moz'
    console.log(anyString.substring(0, 3));
    console.log(anyString.substring(3, 0));
    
    // Displays 'lla'
    console.log(anyString.substring(4, 7));
    console.log(anyString.substring(4));
    console.log(anyString.substring(7, 4));
    
    // Displays 'Mozill'
    console.log(anyString.substring(0, 6));
    
    // Displays 'Mozilla'
    console.log(anyString.substring(0, 7));
    console.log(anyString.substring(0, 10));
    

    7.String.prototype.substr() The substr() method returns the characters in a string beginning at the specified location through the specified number of characters.

    var str = 'abcdefghij';
    
    console.log('(1, 2): '   + str.substr(1, 2));   // '(1, 2): bc'
    console.log('(-3, 2): '  + str.substr(-3, 2));  // '(-3, 2): hi'
    console.log('(-3): '     + str.substr(-3));     // '(-3): hij'
    console.log('(1): '      + str.substr(1));      // '(1): bcdefghij'
    console.log('(-20, 2): ' + str.substr(-20, 2)); // '(-20, 2): ab'
    console.log('(20, 2): '  + str.substr(20, 2));  // '(20, 2): '
    

    8.String.prototype.concat() The concat() method combines the text of one or more strings and returns a new string.

    var hello = 'Hello, ';
    console.log(hello.concat('Kevin', ' have a nice day.'));
    
    /* Hello, Kevin have a nice day. */
    

    String.prototype.toLowerCase() : The toLowerCase() method returns the calling string value converted to lower case.
    String.prototype.toUpperCase() : The toUpperCase() method returns the calling string value converted to upper case.

    相关文章

      网友评论

          本文标题:string 操作总结

          本文链接:https://www.haomeiwen.com/subject/bfudbttx.html