-
字符串的内置功能很多,重要的举例说明
- anchor() 创建HTML锚。
var str='hello,world'
re=str.anchor()
console.log(re)
- big() 用大号字体显示字符串。
- link 将字符串显示为链接。
var str = 'hello,world'
re = str.link()
console.log(re)
- blink() 显示闪动字符串。
- bold() 使用粗体显示字符串。
- charAt() 返回在指定位置的字符。
- charCodeAt() 返回在指定的位置的字符的 Unicode 编码。
- concat() 连接字符串。
- fixed() 以打字机文本显示字符串。
- fontcolor() 使用指定的颜色来显示字符串。
- fontsize() 使用指定的尺寸来显示字符串。
- fromCharCode() 从字符编码创建一个字符串。
- indexOf() 检索字符串。
var str = "ffkwokfvj"
var re1 = str.indexOf("cde") //没有就返回-1
var re2 = str.indexOf("wo", 2) //返回"wo"所在字符串下标,第二个形参表示从哪里开始搜索,没写表示默认从0开始搜索
console.log(re1, re2)
- italics() 使用斜体显示字符串。
- lastIndexOf() 从后向前搜索字符串。
- localeCompare() 用本地特定的顺序来比较两个字符串。
- match() 找到一个或多个正则表达式的匹配。
- replace() 替换与正则表达式匹配的子串。
- search() 检索与正则表达式相匹配的值。
- small() 使用小字号来显示字符串。
- slice() 提取字符串的片断,并在新的字符串中返回被提取的部分。
var str = 'hello,world'
var re = str.slice(3, 5) //选取从3-5的下标的字符串为新字符串,不会改变原数组
console.log(re, str)
- split() 把字符串分割为字符串数组。
var str = 'hello,world'
var re = str.split('r') //以r为对称轴,两端分割
var re1 = str.split("l")//以l为分界线进行分割
console.log(re)
console.log(re1)
- strike() 使用删除线来显示字符串。
- sub() 创建一个sub标签的元素对象(作用把字符串显示为下标。)
var str = 'hello,world'
var re = str.sub()
console.log(re)
- substr() 从起始索引号提取字符串中指定数目的字符。
var str = 'hello,world'
var re = str.substr(7, 2) //第一个形参表示从下标几开始(可以是负数 代表从后往前数)第二个形参表示截取长度
console.log(re)
- substring() 提取字符串中两个指定的索引号之间的字符。
var str = 'hello,world'
var re = str.substring(3, 7) //第一个形参表示开始字符串下标,第二个形参表示结束字符串下标
console.log(re) //打印lo,w
- sup() 把字符串显示为上标。(与sub用法同理)
- toLocaleLowerCase() 把字符串转换为小写。
- toLocaleUpperCase() 把字符串转换为大写。
- toLowerCase() 把字符串转换为小写。(和大写同理)
- toUpperCase() 把字符串转换为大写。
var str = 'hello,world'
var re = str.toUpperCase()
console.log(re)
- toSource() 代表对象的源代码。
- toString() 返回字符串。(将其他类型转化为字符串)
- valueOf() 返回某个字符串对象的原始值。