美文网首页
基础(一) String 对象方法

基础(一) String 对象方法

作者: 一个庸俗的神 | 来源:发表于2017-08-11 03:47 被阅读0次

String 对象 基础 方法 整理

  1. str.charAt(index) 获取字符串索引的值。
var str="Hello world!"
document.write(str.charAt(1))//a
  1. str.fixed()把字符串显示为打字机字体。
    其他常用的有txt.toLowerCase()txt.toUpperCase()txt.sub()txt.sup()
<html>
<body>

<script type="text/javascript">

var txt="Hello World!"

document.write("<p>Big: " + txt.big() + "</p>")
document.write("<p>Small: " + txt.small() + "</p>")

document.write("<p>Bold: " + txt.bold() + "</p>")
document.write("<p>Italic: " + txt.italics() + "</p>")

document.write("<p>Blink: " + txt.blink() + " (does not work in IE)</p>")//显示闪动字符串。
document.write("<p>Fixed: " + txt.fixed() + "</p>")
document.write("<p>Strike: " + txt.strike() + "</p>")

document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>")
document.write("<p>Fontsize: " + txt.fontsize(16) + "</p>")

document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>")
document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>")

document.write("<p>Subscript: " + txt.sub() + "</p>")//把字符串显示为下标。
document.write("<p>Superscript: " + txt.sup() + "</p>")//把字符串显示为上标。

document.write("<p>Link: " + txt.link("http://www.w3school.com.cn") + "</p>")
</script>

</body>
</html>
  1. stringObject.indexOf(searchvalue,fromindex)
    indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
    lastIndexOf() 从后向前搜索字符串。参数与 indexOf() 相同。
  • searchvalue 必需。规定需检索的字符串值。
  • fromindex 可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的首字符开始检索。
    注释:如果要检索的字符串值没有出现,则该方法返回 -1。
var str="Hello world!"
document.write(str.indexOf("Hello") + "<br />")//0
document.write(str.indexOf("World") + "<br />")//-1
document.write(str.indexOf("world"))//6
  1. stringObject.match(searchvalue)
    stringObject.match(regexp)
    match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
    该方法类似 indexOf()lastIndexOf(),但是它返回指定的值,而不是字符串的位置。
  • searchvalue 必需。规定要检索的字符串值。
  • regexp 必需。规定要匹配的模式的 RegExp 对象。如果该参数不是 RegExp 对象,则需要首先把它传递给 RegExp 构造函数,将其转换为 RegExp 对象。
var str="Hello world!"
document.write(str.match("world") + "<br />")//world
document.write(str.match("World") + "<br />")//null
document.write(str.match("worlld") + "<br />")//null
document.write(str.match("world!"))//world!
var str="1 plus 2 equal 3"
document.write(str.match(/\d+/g))//1,2,3
  1. stringObject.replace(regexp/substr,replacement)
    replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
  • regexp/substr 必需。规定子字符串或要替换的模式的 RegExp 对象。请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象。
  • replacement 必需。一个字符串值。规定了替换文本或生成替换文本的函数。
    如果它是字符串,那么每个匹配都将由字符串替换。但是 replacement 中的 $ 字符具有特定的含义。
  1. $1、$2、...、$99 与 regexp 中的第 1 到第 99 个子表达式相匹配的文本。

  2. $& 与 regexp 相匹配的子串。

  3. $ ` 位于匹配子串左侧的文本。

  4. $' 位于匹配子串右侧的文本。

  5. $$ 直接量符号。

  6. stringObject.search(regexp)
    regexp 该参数可以是需要在 stringObject 中检索的子串,也可以是需要检索的 RegExp 对象。
    注释:要执行忽略大小写的检索,请追加标志 i
    返回stringObject 中第一个与 regexp 相匹配的子串的起始位置。
    注释:如果没有找到任何匹配的子串,则返回 -1。

var str="Visit W3School!"
document.write(str.search(/w3school/i))//6
  1. stringObject.substring(start,stop)
    substring() 方法用于提取字符串中介于两个指定下标之间的字符。
  • start 必需。一个非负的整数,规定要提取的子串的第一个字符在 stringObject 中的位置。
  • stop 可选。一个非负的整数,比要提取的子串的最后一个字符在 stringObject 中的位置多 1。
    如果省略该参数,那么返回的子串会一直到字符串的结尾。
var str="Hello world!"
document.write(str.substring(3))//lo world!
  1. stringObject.substr(start,length)
    substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。
  • start 必需。要抽取的子串的起始下标。必须是数值。如果是负数,那么该参数声明从字符串的尾部开始算起的位置。也就是说,-1 指字符串中最后一个字符,-2 指倒数第二个字符,以此类推。
  • length 可选。子串中的字符数。必须是数值。如果省略了该参数,那么返回从 stringObject 的开始位置到结尾的字串。
var str="Hello world!"
var str1 = str.substr(3)//lo world!
var str2 = str.substr(3,7)//lo worl
  1. stringObject.valueOf()
    valueOf() 方法可返回 String 对象的原始值。
    原始值是由从 String 对象下来的所有对象继承的。
    valueOf() 方法通常由 JavaScript 在后台自动进行调用,而不是显式地处于代码中。
  • 当调用该方法的对象不是 String 时抛出 TypeError 异常。
  1. stringObject.anchor(anchorname)
    anchor() 方法用于创建 HTML 锚。
  • anchorname 必需。为锚定义名称。
var txt="Hello world!"
document.write(txt.anchor("myanchor"))
//<a name="myanchor">Hello world!</a>

相关文章

  • 基础(一) String 对象方法

    String 对象 基础 方法 整理 str.charAt(index) 获取字符串索引的值。 str.fixed...

  • String对象

    String对象属性 String对象方法

  • 字符串String

    String 对象 String 对象用于处理文本(字符串)。String 对象创建方法: new String(...

  • string字符串对象

    String 对象 String 对象用于处理文本 创建 String 对象 对象属性 获取字符串长度: 对象方法...

  • TypeScript类型

    TypeScript类型 基础类型 string number boolean 数组 两种定义数组方法: 对象类型...

  • js对象 字符串 数组 Math Date方法

    string Math Array Date方法 浏览器对象 最基础的增删改查 以及JS的小练习 对象 创建...

  • Java集合去重的几种方法

    一、基础数据类型集合去重: 例:String 方法实现: 二、对象根据指定属性去重

  • java基础随笔

    1.equals 方法本质就是 ==。引用对象不重写equals方法就是比较地址值 2.String 不是基础类型...

  • java基础

    一:java基础 1.简述string对象,StringBuffer、StringBuilder区分string是...

  • JS基础-string内置对象

    String 对象用于处理文本(字符串)。String 对象创建方法: new String()。语法: char...

网友评论

      本文标题:基础(一) String 对象方法

      本文链接:https://www.haomeiwen.com/subject/avakrxtx.html