String and String
string
definition
A string is an immutable ordered sequence of 16-bit values, each character usually from the Unicode character set.
The length of a string is the number of 16-bit values it contains
literal
A sequence of characters enclosed in single or double quotes.
Single and double quotes can be nested, with strings delimited by the outermost quotes.
如:“how are ‘you’!”
String literals can be split into several lines. Each line must end with a backslash (\), and backslashes are not included in the content of the string.
let s = "how are \
"you";
console.log(s);
escape character
A character followed by a backslash (\) is used to express special meaning.
如,\’,\”,\n,\r,\\等。
Universal escape characters
represent any character code in Latin-1 or Unicode through hexadecimal numbers.
如:\xA9,表示Latin-1编码的版权符号。“\xA9”->”©”
如:\u03C0,表示Unicode编码的π字符。"\u03c0"->”π”
Common methods
1.str.split(seperator, limit) // Split the string into an array
把一个字符串分割成字符串数组(不改变原字符串)
seperator(可选),指定字符串或正则
limit(可选),指定数组的最大长度
2.str.substring(start, end)://Intercept string
参数均为正,返回从start到end-1的字符,不改变原字符串
3.str.slice(start, end)://Intercept string
两个参数可正可负,负值代表从右截取返回从start到end-1的字符,不改变原字符串
4.str.substr(start, length): //Intercept string.
The start parameter can be positive or negative. A negative number means intercepting from the right without changing the original string.
5. toUpperCase(): //Convert all strings to uppercase
6 , toLowerCase(): //Convert all strings to lowercase
7, str.replace(rgExp/substr, replaceText): //String replacement
返回替换后的字符串,不改变原字符串
8. str.lastIndexOf(searchString, startIndex): //Find string
从右往左查找,无目标字符时返回-1
9. str.indexOf(searchString, startIndex)://Find string
返回字符串第一次出现的位置,从startIndex开始查找,无目标字符时返回-1
10. str.concat(str1, str2)//String concatenation
将str1和str2字符串拼接起来,不改变原字符
11. str.trim(): //Remove spaces before and after the string
var foo = ' bar ';
console.log('('+str.trim()+')'); //(bar)