Common methods for strings
All string methods do not alter the original string
CharAt & charcodeAt retrieves string information based on the index
- CharAt: Gets the character at the specified position according to the index
- CharcodeAt: Get the ASCII (Unicode) value of the specified character
- String. FromCharCode (Decimal Unicode value) : Converts the value to the original character according to the INFORMATION in the ASCII code table, which corresponds to the charCode
Substr && SubString && slice Intercepts characters
- Substr (n,m) : Truncates m entries starting from index n. The second argument is not passed to the end and negative numbers are not supported
- Substring (n, m) : starts from index n and ends at index m (excluding m). Negative numbers are not supported
- Slice (n, m) : slice from index n to index M (excluding M). Negative numbers are supported
let str='zhufengpeixunyangfanqihang'; console.log(str.substr(3, 7)); //=>'fengpei' console.log(str.substring(3, 7)); //=>'feng' console.log(str.substr(3)); //=>'fengpeixunyangfanqihang' intercepted to the end console.log(str.substring(3, 10000)); Console. log(str.substring(3, 7)); //=>'feng' console.log(str.slice(3, 7)); //=>'feng' console.log(str.substring(-7, -3)); //=> "subString does not support negative index console.log(str.slice(-7, -3)); Str.length =>slice(26-7,26-3) =>slice(19,23)Copy the code
ToUpperCase && toLowerCase changes case
- ToUpperCase: Capitalize all letters
- ToLowerCase: All lowercase letters
let str='ZhuFengPeiXunYangFanQiHang'; str = str.toUpperCase(); console.log(str); //=>'ZHUFENGPEIXUNYANGFANQIHANG' str = str.toLowerCase(); console.log(str); / / = > 'zhufengpeixunyangfanqihang / / implementation capitalize the first letter STR = STR., substr (0, 1). The toUpperCase () + STR., substr (1); console.log(str); //=>'Zhufengpeixunyangfanqihang' var abc='abcdefg'; Var res=abc.substring(0,1).touppercase ()+abc.substring(1); console.log(res); //'Abcdefg'Copy the code
IndexOf && lastIndexOf && includes query string
- IndexOf (x,y) : gets the indexOf the first occurrence of x,y is the starting index that controls the search; Without this character, -1 is returned
- LastIndexOf (x) : The index of the last occurrence position, without this character, returns -1
- Includes (“) : Queries whether this character exists in the string. Returns true if it does, false if it does not
let str='zhufengpeixunyangfanqihang'; console.log(str.indexOf('n')); //=>5 console.log(str.lastIndexOf('n')); //=>24 console.log(str.indexOf('@')); / / = > 1 there is no return 1 if (STR) indexOf (' @ ') = = = 1) {/ / does not include the @ this character in the string} the console. The log (STR) indexOf (" feng ")); Console. log(str.indexof ('peiy'))); //=>-1 console.log(str.indexOf('n', 7)); //=>12 search string index 7 and subsequent string, the first occurrence of the position of the index if (! Str.includes ('@')) {console.log(' current string does not contain @'); }Copy the code
Split split a character into an item in an array to specify a character
Corresponds to the join method in numbers
let str='music|movie|eat|sport'; let ary=str.split('|'); //=>["music","movie", "eat", "sport"] str=ary.join(','); console.log(str); //=>"music,movie,eat,sport" var str2='https://www.baidu.com/s?wd=JS&rsv_spt=1&a=1&b=2'; var res3=str2.split('? '); console.log(res3); //["https://www.baidu.com/s", "wd=JS&rsv_spt=1&a=1&b=2"] var ary=res3[1].split('&'); console.log(ary); // ["wd=JS", "rsv_spt=1", "a=1", "b=2"] var obj={}; Ary.foreach ((item,index)=>{var inner=item.split('='); var inner=item.split('='); console.log(inner); //["wd", "JS"] ["rsv_spt", "1"] ["a", "1"] ["b", "2"] obj[inner[0]]=inner[1]; console.log(obj); //{wd: "JS", rsv_spt: "1", a: "1", b: "2"} })Copy the code
Replace character
Str.replace (‘ replaced character ‘, ‘replaced character’)
Let STR =' qomolangma @training @sail @sail '; // str = str.replace('@', '-'); // console.log(str); REPLACE: STR =str.replace(/@/g, '-'); REPLACE: STR =str.replace(/@/g, '-'); console.log(str); //=> Mount Everest - training - sail - set sailCopy the code
Trim trim the front and rear Spaces
Let STR =' Everest '; console.log(str.trim()); / / = > "Everest"Copy the code