The method of the string summary

String methods Summary:

1. length property returns the length of the string

var str = '1234567'
console.log(str.length) // 7

2. indexOf () method returns the index of the specified text string __ __ appears for the first time

3. lastIndexOf () method returns the index of the specified text string appears __ __ final

var str = 'China'
console.log(str.indexOf('China')) // 0  如果是字符串按一个来
console.log(str.indexOf('C')) // 0  返回的是下标 从 0 开始
console.log(str.indexOf('w')) // -1  没有则返回 -1
// 两种方法都接受作为检索起始位置的第二个参数 缩小搜查范围
var str = "The full name of is the People's Republic of China.";
console.log(str.indexOf("China")); // 45   
console.log(str.indexOf("China", 11)); // 45

3. There are three ways to extract part of the string:

  • slice(start, end)
  • substring (start, end) // similar slice () except that the substring () can not accept the negative index.
  • substr (start, length) // similar slice () except that the second parameter is extracted a predetermined length portion. Finally omitted intercepted
var str = "0123456789"
var res = str.slice(1,4)  // 123  注意包括__前__不包括__后__
var res = str.slice(1,-1); //  12345678  // 负值从左数 -1 不包含9 
var res = str.slice(1); //  123456789  // 不写 就接到最后了  
var res = str.slice(-5); //  56789  // 简单记忆 截取后五位  

4. The character string is converted to an array of split ()

var str = "a,b,c,d,e,f";
var arr = str.split(","); // ["a", "b", "c", "d", "e", "f"]
// 反过来  用的是数组的 join(',')  

5. replace () method call it does not change the character string. It returns a new string

  • By default, replace () replaces only the first match:
str = "Please visit Microsoft and Microsoft!" 
var n = str.replace("Microsoft", "W3School") //  "Please visit W3School and Microsoft!"
  • By default, replace () is case sensitive
str = "Please visit Microsoft and Microsoft!" 
var n = str.replace("microsoft", "W3School") //  "Please visit Microsoft and Microsoft!"
  • To replace all occurrences, use a regular expression symbol g (for global search):
str = "Please visit Microsoft and Microsoft!" 
var n = str.replace(/Microsoft/g, "W3School") //  "Please visit W3School and W3School!"

6. Case conversion by toUpperCase () converts a string to upper case by the toLowerCase () to convert strings to lower:

var text1 = "Hello World!"       
var text2 = text1.toLowerCase()  // "hello world!"

Concat 7. The method of splicing string () connected to two or more strings

var text1 = "Hello";
var text2 = "World";
text3 = text1.concat(" ",text2); // Hello World

8. trim () method removes both ends of the string whitespace:

var str = "       Hello World!        ";
alert(str.trim());  //  Hello World!

// Internet Explorer 8 或更低版本不支持 trim() 方法 
// 如需支持 IE 8,您可搭配正则表达式使用 replace() 方法代替:
// var str = "       Hello World!        ";
// alert(str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''));

CharAt string extracted by the subscript (position) charCodeAt (position)

var str = "HELLO WORLD";
str.charAt(0);            // 返回 H
str.charCodeAt(0);         // 返回 72   这是'H'的 unicode 编码

// 最好还是使用 str[0] // 更加直观
* 使用属性访问有点不太靠谱:

  * 不适用 Internet Explorer 7 或更早的版本
  * 它让字符串看起来像是数组(其实并不是)
  * 如果找不到字符,[ ] 返回 undefined,而 charAt() 返回空字符串。
  * 它是只读的。str[0] = "A" 不会产生错误(但也不会工作!) 赋值是无效的

Guess you like

Origin www.cnblogs.com/var-chu/p/11956899.html