replace()方法 定义:用一些字符 替换 另一些字符,或替换一个与正则表达式匹配的字符串 语法:string.replace(searchvalue,newvalue); 注意:该方法 不会改变原有的字符串
var str1 = "Hello World Hello hello" ;
console.log ( str1.replace ( "Hello" , "Red" ) ) ; //Red World Hello hello
console.log ( str1.replace ( /Hello/g, "blue" ) ) ; //blue World blue hello
console.log ( str1.replace ( /Hello/gi, "blue" ) ) ; //blue World blue blue
console.log ( str1) ; //Hello World Hello hello
slice()方法 定义:提取 字符串的某个部分, 并以新的字符串返回被提取的部分 语法:string.slice(start,end)(含头不含尾 ) start:必需,要抽取的片段的起始下标,第一个字符位置为0,如果为负数 ,则从尾部开始截取 end:可选,要截取的片段结尾的下标 注意:不改变 原始字符串
var str2 = "Hello World Hello hello" ;
console.log ( str2.slice ( 3, 7) ) ; //lo w
console.log ( str2.slice ( 3) ) ; //lo World Hello hello
console.log ( str2.slice ( -3) ) ; //llo, 从倒数第三个取到最后一个
console.log ( str2) ; //Hello World Hello hello
split()方法 定义:把一个字符串 分割 成字符串数组 语法:string.split(separator,limit); 注意:如果把空字符串("")用作separator,那么stringObject中的每个字符之间都会被分割 split()方法 不改变 原始字符串
var str3 = "Hello World Hello hello" ;
console.log ( str3.split ( "o" ) ) ; //Hell, W, rld Hell, hell,
console.log ( str3.split ( "" ) ) ; //H, e, l, l, o, , W, o, r, l, d, , H, e, l, l, o, , h, e, l, l, o
console.log ( str3.split ( " " ) ) ; //Hello, World, Hello, hello
console.log ( str3.split ( " " , 2) ) ; //Hello, World
console.log ( str3) ; //Hello World Hello hello
substr()方法 定义:在字符串中 抽取 从开始下标开始的指定数目的字符 语法:string.substr(start,length) 注意:不改变 原来的字符串,它 可以代替substring()和slice()
var str4 = "Hello World!!" ;
console.log ( str4.substr ( 2, 5) ) ; //llo W
console.log ( str4) ; //Hello World!!
substring()方法 定义:用于 提取 字符串中介于两个指定下标之间的字符 语法:string.substring(from,to) from:必选,非负整数 to:可选,非负整数,如果省略则到最后 注意:不改变 原来的字符串, 含头不含尾
var str5 = "Hello World!!" ;
console.log ( str5.substring ( 1, 3) ) ; //el
console.log ( str5) ; //Hello World!!
trim()方法 定义:删除 字符串的 头尾空白符 ,空白符包括:空格、制表符tab、换行符等其他空白符 语法:string.trim() 注意:不会改变 原字符串, 不适用于null、undefined、Number
var str6 = " Hello World!! " ;
console.log ( str6.trim ( ) ) ; //"Hello World!!"
console.log ( str6) ; //" Hello World!! "
toLocaleLowerCase()方法 定义:把字符串 转换为小写 语法:string.toLocaleLowerCase(); 注意:不改变 原字符串
var str7 = "Hello World!!" ;
console.log ( str7.toLocaleLowerCase ( ) ) ; //hello world!!
console.log ( str7) ; //Hello World!!
toLocaleUpperCase()方法 定义:将字符串 转换成大写 语法:string.toLocaleUpperCase(); 注意:不改变 原字符串
var str8 = "Hello World!!" ;
console.log ( str8.toLocaleUpperCase ( ) ) ; //HELLO WORLD!!
console.log ( str8) ; //Hello World!!
toLowerCase()方法 定义:将字符串 转换成小写 ,和toLocaleLowerCase()方法等同 语法:string.toLowerCase(); 注意:不改变 原字符串
var str9 = "Hello World!!" ;
console.log ( str9.toLowerCase ( ) ) ; //hello world!!
console.log ( str9) ; //Hello World!!
toUpperCase()方法 定义:将字符串 转换成大写 ,等同于toLocaleUpperCase()方法 语法:string.toUpperCase() 注意:不改变 原字符串
var str10 = "Hello World!!" ;
console.log ( str10.toUpperCase ( ) ) ; //HELLO WORLD!!
console.log ( str10) ; //Hello World!!
startsWith()方法 定义:检测字符串 是否以指定的字符串开始 语法:string.startsWith(searchvalue,start) start:可选,开始检索的位置,默认为0 注意:如果是以指定的字符串开头返回true,否则false startsWith()方法对 大小写敏感
var str11 = "Hello World Hello hello" ;
console.log ( str11.startsWith ( "Hello" ) ) ; //true
console.log ( str11.startsWith ( "hello" ) ) ; //false
console.log ( str11.startsWith ( "World" , 6) ) ; //true
concat()连接两个或多个字符串 语法:string.concat(string1,string2,string3,…) 注意:不改变 原有的字符串
var str12 = "abcdefghijklmndopq" ;
console.log ( str12.concat ( "rst" , "uvw" , "xyz" ) ) ; //abcdefghijklmndopqrstuvwxyz
console.log ( str12) ; //abcdefghijklmndopq
repeat()方法 定义:字符串 复制 指定次数 语法: string.repeat(count)
var str13 = "Hello World!!" ;
console.log ( str13.repeat ( 3) ) ; //Hello World!!Hello World!!Hello World!!
console.log ( str13) ; //Hello World!!