• String 对象常用方法及属性 详细介绍


    String对象的属性:

      1、constructor  :对创建该对象的函数的引用

      2、length  :  字符串长度

      3、prototype  :允许您向对象添加属性和方法

    String对象的方法:

      1、charAt() 方法可返回指定位置的字符。

        stringObject.charAt(index)  

         index参数必需。表示字符串中某个位置的数字,即字符在字符串中的下标。

    var str="Hello world!"
    console.log(str.charAt(1)) //  e

    2、fixed() 方法用于把字符串显示为打字机字体。

      stringObject.fixed() 

    var str="Hello world!"
    console.log(str.fixed())  //Hello world!

    3、concat() 方法用于连接两个或多个字符串。

      stringObject.concat(stringX,stringX,...,stringX)

      stringX必需。将被连接为一个字符串的一个或多个字符串对象。

      concat() 方法将把它的所有参数转换成字符串,然后按顺序连接到字符串 stringObject 的尾部,并返回连接后的字符串。请注意,stringObject 本身并没有被更改。

    var str1="Hello "
    var str2="world!"
    console.log(str1.concat(str2)) //Hello world!

    4、indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。

      stringObject.indexOf(searchvalue,fromindex)

      searchvalue必需。规定需检索的字符串值。

      fromindex可选参数  

      indexOf() 方法对大小写敏感!

      如果要检索的字符串值没有出现,则该方法返回 -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

    5、lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。

      stringObject.lastIndexOf(searchvalue,fromindex)

      searchvalue必需。规定需检索的字符串值。

      fromindex可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的最后一个字符处开始检索。

      lastIndexOf() 方法对大小写敏感!

      如果要检索的字符串值没有出现,则该方法返回 -1。

    var str="Hello world!"
    document.write(str.lastIndexOf("Hello") + "<br />")  //0
    document.write(str.lastIndexOf("World") + "<br />") //-1
    document.write(str.lastIndexOf("world")) //6 

    6、match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。

      stringObject.match(searchvalue) stringObject.match(regexp)

      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 str1="1 plus 2 equal 3"
    document.write(str1.match(/d+/g))//1,2,3

    7、replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。  

    stringObject.replace(regexp/substr,replacement)
    regexp/substr  必需。规定子字符串或要替换的模式的 RegExp 对象。请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象。
    replacement   必需。一个字符串值。规定了替换文本或生成替换文本的函数。
    ECMAScript v3 规定,replace() 方法的参数 replacement 可以是函数而不是字符串。在这种情况下,每个匹配都调用该函数,它返回的字符串将作为替换文本使用。该函数的第一个参数是匹配模式的字符串。接下来的参数是与模式中的子表达式匹配的字符串,可以有 0 个或多个这样的参数。接下来的参数是一个整数,声明了匹配在 stringObject 中出现的位置。最后一个参数是 stringObject 本身。
    
    
    var str1="Visit Microsoft!"
    document.write(str1.replace(/Microsoft/, "W3School"))//Visit Microsoft!
    var str="Welcome to Microsoft! " str=str + "We are proud to announce that Microsoft has " str=str + "one of the largest Web Developers sites in the world." document.write(str.replace(/Microsoft/g, "W3School"))
    //Welcome to W3School! We are proud to announce that W3School
    //has one of the largest Web Developers sites in the world.

    8、slice() 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。 

    stringObject.slice(start,end)
    start 要抽取的片断的起始下标。如果是负数,则该参数规定的是从字符串的尾部开始算起的位置。也就是说,-1 指字符串的最后一个字符,-2 指倒数第二个字符,以此类推。
    end 紧接着要抽取的片段的结尾的下标。若未指定此参数,则要提取的子串包括 start 到原字符串结尾的字符串。如果该参数是负数,那么它规定的是从字符串的尾部开始算起的位置。
    var str1="Hello happy world!"
    document.write(str1.slice(6)) //happy world!
    var str="Hello happy world!" document.write(str.slice(6,11))//happy

    10、split() 方法用于把一个字符串分割成字符串数组。

    stringObject.split(separator,howmany)
    separator  必需。字符串或正则表达式,从该参数指定的地方分割 stringObject。
    howmany  可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。
    如果把空字符串 ("") 用作 separator,那么 stringObject 中的每个字符之间都会被分割。
      
    var str="How are you doing today?"
    
    document.write(str.split(" ") + "<br />")//How,are,you,doing,today?
    document.write(str.split("") + "<br />")//H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
    document.write(str.split(" ",3))//How,are,you

    11、substring() 方法用于提取字符串中介于两个指定下标之间的字符。  

    stringObject.substring(start,stop)
    start  必需。一个非负的整数,规定要提取的子串的第一个字符在 stringObject 中的位置。
    stop   可选。一个非负的整数,比要提取的子串的最后一个字符在 stringObject 中的位置多 1。如果省略该参数,那么返回的子串会一直到字符串的结尾。
    substring() 不接受负的参数。
    var str="Hello world!"
    document.write(str.substring(3))//lo world!
    var str1="Hello world!" document.write(str1.substring(3,7))//lo w

    12、toLowerCase() 方法用于把字符串转换为小写。 

    stringObject.toLowerCase()
    var str="Hello World!"
    document.write(str.toLowerCase())//hello world!

    13、toUpperCase() 方法用于把字符串转换为大写。

    stringObject.toUpperCase()
    var str="Hello World!"
    document.write(str.toUpperCase())  // HELLO WORLD!

    14、toString() 方法返回字符串。 

    stringObject.toString()
    stringObject 的原始字符串值。一般不会调用该方法。
    当调用该方法的对象不是 String 时抛出 TypeError 异常。

    15、valueOf() 方法可返回 String 对象的原始值。

    stringObject.valueOf()
    当调用该方法的对象不是 String 时抛出 TypeError 异常。
  • 相关阅读:
    C++ 项目和资源导引
    C++ 类再探
    C++ 语句函数再探
    leetcode-174. Dungeon Game 地下城游戏
    34. Find First and Last Position of Element in Sorted Array + 二分
    leetcode-27. Remove Element删除元素
    git 使用入门
    MySQL数据库的启动与停止
    C++类型转换
    C++ 获取对象类型
  • 原文地址:https://www.cnblogs.com/wcx-20151115-hzz/p/10145576.html
Copyright © 2020-2023  润新知