The string method of JavaScript
IndexOf () looks for a character or substring in the specified string, returns the index of the character after a substring
LastIndexOf () finds a character or substring in the specified string from backward to forward
var ZP = "Chinancepuzp";
Console.log (Zp.indexof ("C")); Output 0
Console.log (Zp.indexof ("C", 1)); Start from 1th position backward search back 6
Console.log (Zp.lastindexof ("C")); Output 6
Console.log (Zp.lastindexof ("C", 5)); Start from 5th position forward search returns 0
3. toLowerCase () Returns a copy of the specified string, all lowercase characters
4. toUpperCase () Returns a copy of the specified string, all uppercase characters
var box = "Boxxx";
Console.log (Box.tolowercase ()); Boxxx
Console.log (Box.touppercase ()); Boxxx
5. Trim () Returns a copy of the string with no whitespace characters (spaces in the middle of the string are still there)
var ZP = "1 2 3 4";
Console.log ("|" +zp+ "|"); //| 1 2 3 4 |
Console.log ("|" +zp.trim () + "|"); |-2 3 4|
6. substr () Extract a substring the first argument is the starting position (negative words start at the end) The second parameter is the length that needs to be extracted, substr () extracts the substring from the string, and does not change the original string
7. Substring () Extract a substring the first argument is the starting position (non-negative integer) The second parameter is the end position (non-negative integer)
8. Slice () Extract a substring the first argument is the start position the second argument is the end position
(Note: substr (), substring (), slice () if the second argument is not, from the start position to the end)
var ZP = "Zpazpy";
Extract ZP
Console.log (Zp.substr (0,2));
Console.log (zp.substring (0,2));
Console.log (Zp.slice (0,2));
Console.log (Zp.slice ( -3,-1)); ZP "ZP" in "Zpy"
Console.log (Zp.substr (1,4)); Pazp
Console.log (zp.substring (1,4)); Paz
9. The string has a length property that contains how many characters a string is.
Split () cut a string into an array of strings
The first argument can be a regular or a string
The second parameter is the maximum length of the returned array
var ZP = "Zp=5&zpy=6";
var item = zp.split ("&");
var items = {};
var name = Null,age = null;
for (var i = 0; i < item.length; i++) {
var a = [];
A = Item[i].split ("=");
names = A[0];
age = a[1];
Items[names] = age;
}
Console.log (items); {"ZP": 5, "Zpy": 6}
A second parameter study
var ZP = "Zp=5&z&p&y&=6";
var a = [];
A = Zp.split ("&", 4);
Console.log (a); Zp=5 Z P y
Match () finds one or more regular match results
Returned null was not found, a regular that returned the condition was found
var pattern =/[0-9]/g;
var str = "1234";
Console.log (Str.match (pattern)); return ["1", "2", "3", "4"]
Replace () replaces (one or more) substrings that match a given regular, and returns a string
Replace (regexp,replacement) The first parameter is a regular second parameter is the replacement content
var pattern =/[0-9]/g;
var str = "1234";
var a = [];
Console.log (Str.replace (Pattern, "ZP")); Zpzpzpzp
Search (REGEXP) returns the starting position of the first match to the REGEXP substring according to a regular, if there is no match to return-1
var pattern =/[0-9]+/;
var pattern1 =/[a,d]+/;
var box = "zpzp12345678";
Console.log (Box.search (pattern)); Returns 4
Console.log (Box.search (PATTERN1)); Returns-1
Localecompare () Compares two strings ("A" < "B") in the specified order to compare Unicode encodings (based on ASCII code)
Str.localecompare (target) Str<target, return -1;str>target, return 1; same as indistinguishable, return 0
String methods for JavaScript