字符串的内置功能

66 阅读2分钟
  • 字符串的内置功能很多,重要的举例说明

    1. anchor() 创建HTML锚。
var str='hello,world'

 re=str.anchor()

 console.log(re)

image.png

  1. big() 用大号字体显示字符串。
  2. link 将字符串显示为链接。
  var str = 'hello,world'
  re = str.link()
  console.log(re)

image.png

  1. blink() 显示闪动字符串。
  2. bold() 使用粗体显示字符串。
  3. charAt() 返回在指定位置的字符。
  4. charCodeAt() 返回在指定的位置的字符的 Unicode 编码。
  5. concat() 连接字符串。
  6. fixed() 以打字机文本显示字符串。
  7. fontcolor() 使用指定的颜色来显示字符串。
  8. fontsize() 使用指定的尺寸来显示字符串。
  9. fromCharCode() 从字符编码创建一个字符串。
  10. indexOf() 检索字符串。
var str = "ffkwokfvj"
  var re1 = str.indexOf("cde") //没有就返回-1
  var re2 = str.indexOf("wo", 2) //返回"wo"所在字符串下标,第二个形参表示从哪里开始搜索,没写表示默认从0开始搜索
  console.log(re1, re2)

image.png

  1. italics() 使用斜体显示字符串。
  2. lastIndexOf() 从后向前搜索字符串。
  3. localeCompare() 用本地特定的顺序来比较两个字符串。
  4. match() 找到一个或多个正则表达式的匹配。
  5. replace() 替换与正则表达式匹配的子串。
  6. search() 检索与正则表达式相匹配的值。
  7. small() 使用小字号来显示字符串。
  8. slice() 提取字符串的片断,并在新的字符串中返回被提取的部分。
  var str = 'hello,world'
  var re = str.slice(3, 5) //选取从3-5的下标的字符串为新字符串,不会改变原数组
  console.log(re, str)

image.png

  1. split() 把字符串分割为字符串数组。
 var str = 'hello,world'
  var re = str.split('r') //以r为对称轴,两端分割
  var re1 = str.split("l")//以l为分界线进行分割
  console.log(re)
  console.log(re1)

image.png

  1. strike() 使用删除线来显示字符串。
  2. sub() 创建一个sub标签的元素对象(作用把字符串显示为下标。)
  var str = 'hello,world'
  var re = str.sub() 
  console.log(re)

image.png

  1. substr() 从起始索引号提取字符串中指定数目的字符。
  var str = 'hello,world'
  var re = str.substr(7, 2) //第一个形参表示从下标几开始(可以是负数 代表从后往前数)第二个形参表示截取长度
  console.log(re) 

image.png

  1. substring() 提取字符串中两个指定的索引号之间的字符。
 var str = 'hello,world'
  var re = str.substring(3, 7) //第一个形参表示开始字符串下标,第二个形参表示结束字符串下标
  console.log(re) //打印lo,w

image.png

  1. sup() 把字符串显示为上标。(与sub用法同理)
  2. toLocaleLowerCase() 把字符串转换为小写。
  3. toLocaleUpperCase() 把字符串转换为大写。
  4. toLowerCase() 把字符串转换为小写。(和大写同理)
  5. toUpperCase() 把字符串转换为大写。
 var str = 'hello,world'
  var re = str.toUpperCase()
  console.log(re)

image.png

  1. toSource() 代表对象的源代码。
  2. toString() 返回字符串。(将其他类型转化为字符串)
  3. valueOf() 返回某个字符串对象的原始值。