Common Methods of String in Javascript: instance analysis and string instance analysis
This article describes the common methods of String in Javascript. Share it with you for your reference. The details are as follows:
// Length attribute: obtains the number of characters in a string. Var s = 'Love is like a gust of Wind'; alert (s. length); // charAt (index) method: gets the character at the specified index position. The index starts from 0. var s1 = 'I don't miss you anymore'; alert (s1.charAt (4 )); /// // indexOf ('E', startIndex) method: Obtain the position where the specified string appears for the first time. StartIndex indicates the number of times to start searching. Var s2 = 'after knowing it, I think it has passed another autumn'; alert (s2.indexOf ('Ta', 3); // 6 // split ('delimiter ', returns a string as an Array Based on the delimiter. // Limit indicates the maximum length of the array to be returned (customizable ). Var s3 = 'horse | in the rivers and lakes | kill '; alert (s3.split (' | ', 2); // use | as the separator to split the string and display it as an array, and only contains two elements var arr = s3.split ('|', 3); // three elements for (var I = 0; I <arr. length; I ++) {alert (arr [I]) ;}// substr (startIndex, len) starts from startIndex (inclusive), and then intercepts len characters. Var s4 = 'one-piece Black url'; alert (s4.substr (); // sweater, which starts from subscript 4 and contains 4. capture two characters later. // substring (startIndex, stopIndex) starting from startIndex, // capture to the stopIndex position (excluding the character of stopIndex) var s5 = 'fireworks easy-to-use easy-to-split '; alert (s5.substring )); // easy to use, starting from 1 to 4, excluding 1 and 4 // toUpperCase () converts uppercase and toLowerCase (); convert lowercase var s6 = 'what are you kidding me'; alert (s6.toUpperCase (); alert (s6.toLowerCase ());
I hope this article will help you design javascript programs.