程序员全栈笔记让前端飞

JavaScript 字符串常用方法总结

2018-12-25  本文已影响10人  小贤笔记

文章摘自 《JavaScript高级程序设计(第3版)》 及 菜鸟教程

一. 字符方法

charAt()

charCodeAt()

二. 字符串操作方法

concat()

slice()

substr()

substring()

三. 字符串位置方法

indexOf()

lastIndexOf()

四. 字符串大小写转换方法

toLowerCase()

toUpperCase()

五. 字符串的模式匹配方法

match()

search()

replace()

split()

六. 其他

trim()

let stringValue = "    hello world    ";

stringValue.trim(); // "hello world"

localeCompare()

let stringValue = "bcd";

stringValue.localeCompare("abc"); // 1
stringValue.localeCompare("bc"); // 1
stringValue.localeCompare("bcd"); // 0
stringValue.localeCompare("bcde"); // -1
stringValue.localeCompare("cd"); // -1
stringValue.localeCompare("Bcd"); // -1

formCharCode()

String.fromCharCode(104, 101, 108, 108, 111); // "hello"
上一篇 下一篇

猜你喜欢

热点阅读