1、关于String的属性和方法
String做为JavaScript的内置函数,String.prototype继承于Object.prototype,同时String.prototype上内置了不少方法:正则表达式
nchor()、big()、blink()、bold()、charAt()、charCodeAt()、codePointAt()、concat()、String()、endsWith()、fixed()、 fontcolor()、fontsize()、includes()、indexOf()、italics()、 lastIndexOf()、length、link()、localeCompare()、match()、 normalize()、padEnd()、padStart()、repeat()、replace()、search()、slice()、small()、split()、startsWith()、strike()、sub()、substr()、 substring()、sup()、toLocaleLowerCase()、toLocaleUpperCase()、toLowerCase()、toString()、toUpperCase()、trim()、trimLeft()、 trimRight()、valueOf()、[Symbol.iterator]()、__proto__: Object
var str = "abc123ABC"; var str1 = "Concat1"; var str2 = "Concat2";
一、字符串的length属性数组
console.log(str.length); // 9
二、字符串的方法
a. concat函数
console.log(str.concat(str1, str2)); // abc123ABCConcat1Concat2
==> 返回新的字符,不改变原字符串this
b. slice(start,end)spa
console.log(string.slice(0, 3)); // abc ==> 下标[0, 3) console.log(string.slice(-3)); // ABC ==> 倒数第3位到结束 console.log(string.slice(-6, -3)); // 123 ==> 倒数下标[-6, -3)
==> slice(start,end)==>返回一个新的字符串。包括字符串 stringObject 从 start 开始(包括 start)到 end 结束(不包括 end)为止的全部字符。当没有第二个参数时,截止到最后。prototype
==> 当只有一个参数,而且这个参数为负数时,表示从字符串尾部开始,-1表示倒数第一开始,n表示倒数第n开始到最后。code
c. substring(start,end)regexp
console.log(str.substring(0, 3)); // abc ==> 下标[0, 3) console.log(str.substring(-2, 3)); // abc ==> -2转化为0,下标[0, 3) console.log(str.substring(-2, -3)); // abc ==> -2和-3转化为0,为空字符串 // substring和slice差很少,只是不支持负数,转换为0
d. substr(start,lenght)orm
console.log(str.substr(0, 5)); // abc12 console.log(str.substr(-3, 5)); // ABC console.log(str.substr(-3)); // ABC console.log(str.substr(-3, -5)); // ABC
//和上面两个不一样的是substr的第二个参数表示截取字符串的长度,若是只有一个参数和slice相同,若是第二个参数为为负数转换为0对象
//还要注意的是,String.slice() 与 Array.slice() 类似。
对以上三个方法的总结:
一、只接受两个参数,返回新的字符串,不改变原字符串
二、slice ==> 参数能够为负
subString ==> 参数都不能够为负,自动转换为0
subStr ==> 第一个参数能够为负,第二个参数表示的是字符串的长度,不能为负数,自动转换为0
e. indexOf(string, start)和lastIndexOf(string, start)
indexOf搜索目标字符串的开始索引,第一个参数只能是字符串,且对大小写敏感,只返回第一个的位置索引; indexOf第二个参数表示从该索引开始搜索,只能为正数,负数自动转化为0。
lastIndexOf从后往前搜索目标字符串的开始索引,第一个参数只能是字符串,且对大小写敏感,只返回最后一个的位置索引; indexOf第二个参数只能为正数,负数返回结果为-1找不到。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的最后一个字符处开始检索。
var str = '123abcABC123abcABC123abcABC123abcABC'; console.log(str.indexOf('abc')); // 3 console.log(str.indexOf('dddd')); // -1 ==> 找不到 str.indexOf('abc', 6); // 12 str.indexOf('abc', -6); // 3 str.lastIndexOf('abc', 4); // 3 str.lastIndexOf('abc', -4); // -1 str.lastIndexOf('abc', 14); // 12
f. search(sting/RegExp)
搜索目标字符串的开始索引,参数能够是字符串或者正则,对大小写敏感,正则能够忽略大小写敏感。 只返回第一个的位置索引,因此即便是全局匹配g也是返回第一个的位置索引; 特别注意:search方法只接收一个参数。
var str = '123abcABC123abcABC123abcABC123abcABC'; console.log(str.search('abc')); // 3 console.log(str.search(/Abc/ig)); // 3 console.log(str.search(/Abc/ig, 6)); // 3 console.log(str.search('dddd')); // -1 ==> 找不到
indexOf:不支持正则,支持指定起始位置开始搜索(负数时转化为0),有lastIndex反向搜索 search:支持正则和字符串,不支持指定起始位置开始搜索
g. match(searchvalue, regexp)
match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
该方法相似 indexOf() 和 lastIndexOf(),可是它返回指定的值,而不是字符串的位置。
searchvalue 必需。规定要检索的字符串值。
regexp 必需。规定要匹配的模式的 RegExp 对象。若是该参数不是 RegExp 对象,则须要首先把它传递给 RegExp 构造函数,将其转换为 RegExp 对象。
var str = '123abcABC123abcABC123abcABC123abcABC'; console.log(str.match('abc')); // ["abc"] console.log(str.match(/abc/)); // ["abc"] console.log(str.match(/abc/g)); // ["abc", "abc", "abc", "abc"] console.log(str.match(/\d+/g)); // ["123", "123", "123", "123"]
h. replace(str/RegExp, repalcement) 第一个参数时字符串或正则,字符串只能替换第一个,正则能够匹配设置(ig); 第二个参数repalcement若是是字符串那么就只接替换,repalcement能够是函数。
var str = '123abcABC123abcABC123abcABC123abcABC'; console.log(str.replace('abc', 'RRR')); // "123RRRABC123abcABC123abcABC123abcABC" console.log(str.replace(/abc/ig, 'RRR')); // "123RRRRRR123RRRRRR123RRRRRR123RRRRRR"
i. trim() ==> IE9如下没有这个方法,要作兼容,以下:
//console.log("trim()==>空格处理:") String.prototype.ltrim=function() { return this.replace(/^\s*/g,""); }; String.prototype.rtrim=function() { return this.replace(/\s*$/g,""); }; String.prototype.trim=function() { return this.replace(/(^\s*)|(\s*$)/g,""); };
j. 字符串比较
log(str.localeCompare(str1)); //-1 log(str1.localeCompare(str)); //1 log(str.localeCompare(str)); // 0
k. 字符串大小写转换
log(str.toUpperCase()); // ABC123ABC log(str.toLocaleUpperCase()); // ABC123ABC log(str.toLowerCase()); // abc123abc log(str.toLocaleLowerCase()); // abc123abc
l. split ==> 将字符串转换为数组
var arr = str.split(''); console.log(arr); //["a", "b", "c", "1", "2", "3", "A", "B", "C"]
join ==> 将数组转换为字符串
console.log(arr.join('')); // abc123ABC
2、支持正则表达式的 String 对象的方法
search、match、replace、split