Javascript Reading Notes: common string Methods

Source: Internet
Author: User

Javascript Reading Notes: common string Methods
Concat () connects multiple strings and returns the merged strings. 1 var s1 = "a"; 2 var s2 = "B"; 3 var s3 = "c"; 4 5 console. log (s1.concat (s2, s3); // the result of the abcconcat () method is equivalent to: result = s1 + s2 +... + sN. If any parameter is not a string, it will be converted to a string before connection. Concat () in the array: add the parameter as an array element and return a new array. 1 var arr = [1, 2, 3]; 2 console. log (arr. concat (4, 5); // [1, 2, 3, 4, 5] indexOf () searches for the index of the first occurrence of a substring. 1 var s = "abcd"; 2 console. log (s. indexOf ("B"); // 1 if no match exists,-1 is returned. 1 var s = "abcd"; 2 console. log (s. indexOf ("e"); //-1 You can pass in the second parameter: fromIndex, indicating that the query starts from the index position fromIndex. By default, the query starts from the start index 0; if fromIndex is negative, the query starts from index 0. 1 var s = "aba"; 2 console. log (s. indexOf ("a", 0); // 03 console. log (s. indexOf ("a", 1); // 24 console. log (s. indexOf ("a",-1); // 0 this method is case sensitive. 1 var s = "Aba"; 2 console. log (s. indexOf ("a"); // 2 lastIndexOf () is similar to indexOf (), but searches from right to left. 1 var s = "aba"; 2 console. log (s. lastIndexOf ("a"); // 2 You can also specify the start index, which starts from the maximum index by default. 1 var s = "aba"; 2 console. log (s. lastIndexOf ("a", 1); // 0 charAt () returns the character at the specified index position (because Javascript does not have a character type, a string of 1 is returned ). 1 var s = "abc"; 2 console. log (s. charAt (1); // If index B is out of bounds, an empty string is returned. 1 var s = "abc"; 2 console. log (s. charAt (-1); // "" substr () substr (fromIndex, length): truncates the length string from the start index fromIndex. 1 var s = "abc"; 2 console. log (s. substr (); // If length is <1, an empty string is returned. 1 var s = "abc"; 2 console. log (s. substr (1,-1); // "if the length is not specified or exceeds the maximum length that can be truncated, It is truncated to the end. 1 var s = "abc"; 2 console. log (s. substr (1); // bc3 console. log (s. substr (); // If the start index of bc is negative, it is intercepted from right to left (from right to left ). 1 var s = "abc"; 2 console. log (s. substr (-); // csubstring () substring (startIndex, endIndex): truncates the substring from the start index startIndex to the end index endIndex. The result contains the characters at startIndex, it does not contain the characters at the endIndex. 1 var s = "abc"; 2 console. log (s. substring (1, 2); // B. If startIndex or endIndex is negative, it is replaced with 0. 1 var s = "abc"; 2 console. log (s. substring (-); // AB returns an empty string if startIndex and endIndex are equal. If startIndex> endIndex, the two values are exchanged during method execution. 1 var s = "abc"; 2 console. log (s. substring (); // B. If endIndex is not specified or the endIndex exceeds the maximum index, it is truncated to the end. 1 var s = "abc"; 2 console. log (s. substring (1); // bc3 console. log (s. substring (); // bcslice () slice (startIndex, endIndex): truncates the substring from the start index startIndex to the end index endIndex. The result contains the characters at startIndex, it does not contain the characters at the endIndex. 1 var s = "abc"; 2 console. log (s. slice (); // If startIndex or endIndex is negative, the index is calculated from right to left (different from substring ). Copy the Code 1 var s = "abc"; 2 console. log (s. slice (1, 2); // b3 // equivalent to 4 console. log (s. slice (1,-1); // b5 // equivalent to 6 console. log (s. slice (-2,-1); // b7 // equivalent to 8 console. log (s. slice (-); // B copy the Code. If the index location indicated by startIndex is equal to the index location indicated by endIndex, or the index position indicated by startIndex is after the index position indicated by endIndex, an empty string (different from substring) is returned ). 1 var s = "abc"; 2 console. log (s. slice (1, 1); // "3 console. log (s. slice (-1,-2); // "" split () is separated by a given string and returns an array of strings after the split. 1 var s = "a, bc, d"; 2 console. log (s. split (","); // ["a", "bc", "d"] 3 4 s = "a1b1c1d1"; 5 console. log (s. split ("1"); // ["a", "B", "c", "d", ""]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.