一、charAt()
接收一个参数,基于0的字符位置。以单字符串的形式返回给定位置的那个字符。javascript
var stringValue = "hello world"; console.log(stringValue.charAt(1)); //"e"
二、charCodeAt()
接收一个参数,基于0的字符位置。 返回的是字符编码。java
var stringValue = "hello world"; console.log(stringValue.charCodeAt(1)); //101
一、concat()
用于将一个或多个字符串拼接起来,返回拼接获得的新字符串,不会修改字符串自己的值,只是返回一个基本类型的字符串值。正则表达式
var stringValue = "hello "; var result = stringValue.concat("world"); console.log(result); // "hello world" console.log(stringValue); // "hello"
二、slice()
截取字符串,只是返回一个基本类型的字符串值,对原始字符串没有任何影响。
若是传两个参数,第一个参数是开始截取的位置,第二个参数是结束截取的位置。函数
var stringValue = "hello world"; console.log(stringValue.slice(3)); //"lo world" console.log(stringValue.slice(3,7)); //"lo w"
三、substring()
截取字符串,只是返回一个基本类型的字符串值,对原始字符串没有任何影响。
若是传两个参数,第一个参数是开始截取的位置,第二个参数是结束截取的位置。编码
var stringValue = "hello world"; console.log(stringValue.substring(3)); //"lo world" console.log(stringValue.substring(3,7)); //"lo w"
四、substr()
截取字符串,只是返回一个基本类型的字符串值,对原始字符串没有任何影响。
若是传两个参数,第一个参数是开始截取的位置,第二个参数是返回的字符个数。code
var stringValue = "hello world"; console.log(stringValue.substr(3)); //"lo world" console.log(stringValue.substr(3,7)); //"lo worl"
一、indexOf()
接收一个参数的时候,返回第一次出现该字符的位置
接收两个参数的时候,第一个是查找的字符,第二个是开始查找的位置。对象
var stringValue = "hello world"; console.log(stringValue.indexOf("o")); //4 console.log(stringValue.indexOf("o",6)); //7
二、lastIndexOf()
接收一个参数的时候,返回最后一次出现该字符的位置
接收两个参数的时候,第一个是查找的字符,第二个是开始查找的位置。索引
var stringValue = "hello world"; console.log(stringValue.lastIndexOf("o")); //7 console.log(stringValue.lastIndexOf("o",6)); //4
这个方法会建立一个字符串的副本,删除前置及后缀的全部空格,而后返回结果。ip
var stringValue = " hello world "; var trimmedStringValue = stringValue.trim(); console.log(stringValue); //" hello world " console.log(trimmedStringValue); //"hello world"
一、toLowerCase()
将字符串转换成小写字符串
var stringValue = "HELLO WORLD"; console.log(stringValue.toLowerCase()); //"hello world"
二、toUpperCase()
将字符串转换成大写
var stringValue = "hello world"; console.log(stringValue.toUpplerCase()); //"HELLO WORLD"
一、match()
只接受一个参数,要么是一个正则表达式,要么是一个RegExp对象。
var text = "cat,bat,sat,fat"; var pattern = /.at/; var matches = text.match(pattern); console.log(maches.index); //0 console.log(maches[0]); //"cat"
二、search()
惟一参数与match()方法参数相同,search()方法返回字符串中第一个匹配项的索引;若是没有找到匹配项,则返回-1.
var text = "cat, bat, sat, fat"; var pos = text.search(/at/); console.log(pos); //1
三、replace()
这个方法接收两个参数:第一个参数能够使一个RegExp对象或者一个字符串,第二个参数能够使一个字符串或者一个函数。
var text = "cat, bat, sat, fat"; var result = text.replace("at","ond"); console.log(result); //"cond, bat, sat, fat" result = text.replace(/at/g,"ond"); console.log(result); //"cond, bond, sond, fond"
这个方法比较两个字符串,并返回下列值中的一个:
var stringValue = "yellow"; console.log(stringValue.localeCompare("brick")); //1 console.log(stringValue.localeCompare("yellow")); //0 console.log(stringValue.localeCompare("zoo")); //-1
这个方法的任务是接收一个或者多个字符编码,而后将它们转换成一个字符串。
console.log(String.fromCharCode(104,101,108,108,111)); //"hello"