javascript字符串

ES5

方法描述详细内容
charAt()返回指定索引位置的字符string.charAt(index)
charCodeAt()返回指定索引位置字符的 Unicode 值string.charCodeAt(index)
concat()连接两个或多个字符串,返回连接后的字符串string.concat(stringX,stringX,…,stringX)
fromCharCode()将 Unicode 转换为字符串String.fromCharCode(n1, n2, …, nX)
indexOf()返回字符串中检索指定字符第一次出现的位置string.indexOf(searchvalue,fromindex)
lastIndexOf()返回字符串中检索指定字符最后一次出现的位置stringObject.lastIndexOf(searchvalue,fromindex)
localeCompare()用本地特定的顺序来比较两个字符串stringObject.localeCompare(target)
match()找到一个或多个正则表达式的匹配stringObject.match(searchvalue) stringObject.match(regexp)
replace()替换与正则表达式匹配的子串stringObject.replace(regexp/substr,replacement)
search()检索与正则表达式相匹配的值stringObject.search(regexp)
slice()提取字符串的片断,并在新的字符串中返回被提取的部分arrayObject.slice(start,end)
split()把字符串分割为子字符串数组stringObject.split(separator,howmany)
substr()从起始索引号提取字符串中指定数目的字符stringObject.substr(start,length)
substring()提取字符串中两个指定的索引号之间的字符stringObject.substring(start,stop)
toLocaleLowerCase()根据主机的语言环境把字符串转换为小写,只有几种语言(如土耳其语)具有地方特有的大小写映射stringObject.toLocaleLowerCase()
toLocaleUpperCase()根据主机的语言环境把字符串转换为大写,只有几种语言(如土耳其语)具有地方特有的大小写映射stringObject.toLocaleUpperCase()
toLowerCase()把字符串转换为小写stringObject.toLowerCase()
toString()返回字符串对象值NumberObject.toString(radix)
toUpperCase()把字符串转换为大写stringObject.toUpperCase()
trim()移除字符串首尾空白string.trim()
valueOf()返回某个字符串对象的原始值mathObject.valueOf()

例子:

(1)
let x="abcde";
console.log(x.charAt(0)); // a
console.log(x.charCodeAt(0)); // 97

(2)
let x="abcde";
let y="xx";
console.log(x.concat(y)) // abcedxx

(3)
console.log(String.fromCharCode(66)) // B

(4)
let x="abcde";
console.log(x.indexOf("c")) // 2
console.log(x.lastIndexOf("d")) // 3

(5)
let x="老虎";
let y="大象";
console.log(x.charCodeAt(0))
console.log(y.charCodeAt(0))
console.log(x.localeCompare(y)) // 1
这里需要说明一下,localeCompare主要是用于比较本地的规则,那么本地的规则是什么呢?简单的讲,就是
比较两个大小,比如,x是老虎,y是大象,转换为特定编码后,x要比y大,所以,最后返回就是1,如果相等,就是返回0,如果是小于,就是返回-1;

(6)
在这里插入图片描述
(7)

let x="abcedfa";
console.log(x.slice(2,4)) //ce

(8)

let x="abcedfa";
console.log(x.split("",4)) //["a","b","c","e"]

(9)

let x="abcedfa";
console.log(x.substr(3,2)) //ed
console.log(x.substring(3,5)) //ed

(10)

let x="abcedfa";
console.log(x.toLocaleLowerCase()) //abcedfa
console.log(x.toLocaleUpperCase()) //ABCEDFA
console.log(x.toLowerCase()) //abcedfa
console.log(x.toUpperCase()) //ABCEDFA

ES6

方法描述内容详细
includes()返回布尔值,判断是否找到参数字符串arr.includes(searchElement) arr.includes(searchElement, fromIndex)
startsWith()返回布尔值,判断参数字符串是否在原字符串的头部string.startsWith(searchvalue, start)
endsWith()返回布尔值,判断参数字符串是否在原字符串的尾部string.endsWith(searchvalue, start)
repeat()方法字符串复制指定次数。string.repeat(count)
padStart()返回新的字符串,表示用参数字符串从头部(左侧)补全原字符串。padStart(targetLength) padStart(targetLength, padString)
padEnd()返回新的字符串,表示用参数字符串从尾部(右侧)补全原字符串。str.padEnd(targetLength [, padString])

例子

let x="bb abcedf a cc";
console.log(x.includes("c")) //true
console.log(x.startsWith("a")) //false
console.log(x.endsWith("c")) //true
console.log(x.repeat(2)) //bb abcedf a ccbb abcedf a cc
console.log(x.padStart(20,"22")) //222222bb abcedf a cc
console.log(x.padEnd(20,"22")) //bb abcedf a cc222222
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李卓书

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值