String
A string is one or more characters that are arranged together in single or double quotes.
' ABC '
"ABC"
Length Property
The string in JS is similar to an array, and is made up of a single character, so you can use the Length property to get the string
var str = "Hello"
Str.length; 5
Some methods commonly used in strings
1. CharAt ()
=> returns the nth character of the string, or an empty string if it is not between 0~str.length-1.
var str = "JavaScript";
Str.charat (2); ' V '
Str.charat (12);
2. IndexOf ()
=> returns the first occurrence of the substr in string str, starting at the start position, or 1 if it does not exist.
Start can be any integer, and the default value is 0. If start < 0 finds the entire string (as if it were passed in 0). If start >= str.length, the method returns-1, unless the string being looked up is an empty string, then the Str.length is returned.
var str = "JavaScript";
Str.indexof (' s '); 1
str.indexof (' s ', 6);//-1
str.indexof (', one);//
Str.indexof (', 8);//8
3. LastIndexOf ()
LastIndexOf (Substr[,start])
= > Returns the last occurrence of substr in string str, starting from the start position, or 1 if it does not exist.
' Lastindex '. LastIndexOf (' a '); 1
4. SUBSTRING ()
Str.substring (start[, end])
= > Returns the characters from start to end (not included), and the start and end are non-negative integers. If the ending argument (end) is omitted, it is intercepted from the start position to the last.
var str = ' ABCDEFG ';
Str.substring (1, 4); "BCD"
str.substring (1);//"BCDEFG"
str.substring ( -1);//"ABCDEFG" is treated as 0 when passed in negative values
5. Slice ()
= > Returns characters from start to end (not included), which can be negative
var str = ' This is awesome ';
Str.slice (4,-1); ' Is Awesom '
6. substr ()
= > Returns a substring in str from the specified position to the specified length, and start can be negative
var str = "Just give me a reason";
STR.SUBSTR (5, 10); "Give me A"
str.substr (-4, 2); "As"
7. Replace ()
Str.replace (Regexp|substr, newsubstr|function)
= > Replace the substring of STR
var str = "Do With Love Me";
Str.replace (' Love ', ' hate '); "Do You Hate Me"
8. Search ()
= > Find str matches a regular expression. Returns the index of the first occurrence of the regular expression in the string if the match succeeds, or returns-1. If the argument passes in a non-regular expression object, it is implicitly converted to a regular expression object using new RegExp (obj)
var str = ' I love javascript! ';
Str.search (/java/); -1
Str.search (/java/);//7
Str.search (/java/i);//7
str.search (' Java ');//7
9. Match ()
= > Returns an array that contains the matching result, or null if there is no match. If the argument passes in a non-regular expression object, it is implicitly converted to a regular expression object using new RegExp (obj)
var str = ' Javascript java ';
Str.match (/java/); ["Java"]
Str.match (/JAVA/GI);//["Java", "Java"]
Str.match (/ab/g);//null
Split ()
Str.split ([separator][, limit])
= > Returns an array in which the delimiter separator can be a string or regular expression
var str = "Hello?" world! ";
Str.split (); [Hello?] world! "]
Str.split ("); ["H", "E", "L", "L", "O", "?", "W", "O", "R", "L", "D", "!"]
Str.split ('? '); ["Hello", "world!"]
Str.split (', 5); ["H", "E", "L", "L", "O"]
Trim ()
= > Remove white space characters at the beginning and end of STR, return a copy of STR without affecting the value of the string itself
var str = ' abc ';
Str.trim (); ' ABC '
console.log (str);//' abc '
toLowerCase ()
= > Converts str to lowercase and returns a copy of Str that does not affect the value of the string itself
var str = ' JavaScript ';
Str.tolowercase (); ' JavaScript '
console.log (str);//' JavaScript '
toUpperCase ()
= > Converts str to uppercase and returns a copy of Str that does not affect the value of the string itself
var str = ' JavaScript ';
Str.touppercase (); ' JavaScript '
console.log (str);//' JavaScript '
The above is a small set to introduce the JavaScript string commonly used methods to explain, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!