JavaScript字符串(String)

一、创建字符串的方法 String

    一  var str=""             string
    二  var str=String()   string
    三  var str=new String() 返回值Object

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

var a="12345"
document.write(a.chart(2))//3

三  、charCodeAt() 方法可返回指定位置的字符的 Unicode 编码。这个返回值是 0 - 65535 之间的整数。

var a="12345"
document.write(a.charCodeAt(0))//49

四、 fromCharCode()可接受一个指定的 Unicode 值,然后返回一个字符串。

document.write(String.fromCharCode(72,69,76,76,79))//HELLO
document.write(String.fromCharCode(65,66,67))//ABC

、indexOf()  方法可返回某个指定的字符串值在字符串中首次出现的位置。
var str="Hello world!"
document.write(str.indexOf("Hello") + "<br />")//0 找H
document.write(str.indexOf("World") + "<br />")//-1 找不到 大小写
document.write(str.indexOf("world"))//6// 找w在哪

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

 
             var str="Hello world!"
            document.write(str.lastIndexOf("l") + "<br />")//找到最后l出现的位置9
            document.write(str.lastIndexOf("World") + "<br />")//-1 找不到 大小写
            document.write(str.lastIndexOf("o"))//找到最后o出现的位置 7

七、search()方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。用在正则。

  var str="Visit W3School!"
   document.write(str.search(/W3School/i))//6

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

       var a="123452"
     document.write(a.replace(/2/g,8))//183452

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

                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=new String ("li jia yang jia")
alert(str.match(/jia/g))//打印出全部jia

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

      var str="Hello world!"
document.write(str.substring(3)+"<br>")//打印下标3后面的所有
document.write(str.substring(3,6))//打印下标3到下标6的 结尾不算  不能为负数

十 一、substr()方法可在字符串中抽取从 start 下标开始的指定数目的字符。
         var str="Hello world!" 
       document.write(str.substr(2)+"<br>")//llo world!
       document.write(str.substr(2,1))//l

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

var str="How are you doing today?"
document.write(str.split() + "<br />")//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

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

var str="haha haha"
var sty="hehe hehe"
document.write(str.concat(sty)+ "<br />")//haha hahahehe hehe
document.write(str+sty)//haha hahahehe hehe

十四、slice() 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。
  var str="Helloworld" 
document.write(str.slice(0,2))//He 结尾不算  参数也可以为负数

十五、toLowerCase() 方法用于把字符串转换为小写。toUpperCase() 方法用于把字符串转换为大写。

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

var str="Hello world!"
document.write(str.toUpperCase())//HELLO WORLD!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值