JavaScript笔记 Day03 字符串

 Day03   字符串


一.字符串操作
1.字符串创建
var s1="hello"
        var s2=new String("hello")

2.属性
s1.length  获取字符串长度

        var s1="hello"

        var s2=new String("hello")

        console.log(s1.length)      // 5

3.方法
(1) 获取字符
        charAt()  返回给定的位置的字符

                var s1="hello"
                s1.charAt(1) //e
                s1.charAt(4)  //o

        charCodeAt()  返回字符编码  ASCII码

        a  97 ------z  122
        A  65------Z  90
        0  48-------9  57
        回车  13
       空格  32

        [ ]  数组      var s1="hello" 
                          01234  ----索引或下标值

         var s1="hello"

        console.log(s1.charCodeAt(0))     // 104

        console.log(s1.charCodeAt(2))     // 108

(2)拼接字符串
        +  字符串连 接
        concat():用于将一个或多个字符串拼接起来,并返回拼接后新得到的字符串
        var s1="hello"
        var s2=s1.concat("world")
        console.log(s2);   //helloworld


(3)子字符串
        substr()
        substr(起始位置,长度)  从起始位置截取指定长度的子字符串
        如起始位置为负数,则倒着数

        s1="hello world"
        s1.substr(3) //lo world   3位开始
        s1.substr(3,6) //lo wor     (3,6)6指的是长度  3开始  长度6
        s1.substr(0) //hello world
        s1.substr(-3) //rld
        s1.substr(3,-4)  //""  长度不能是负数  
        s1.substr(8,3) //rld

        substring(起始位置,终止位置):起始包含,终止不包含
        遇到负数自动转换为0,起始如大于终止,则自动调换位置 

        s1="hello world"
        s1.substring(3) //lo world
        s1.substring(3,7) //lo w
        s1.substring(0) //hello world
        s1.substring(-3)//hello world
        s1.substring(3,-4)// hel   不认负数 相当于(3,0)  
        s1.substring(8,3)//lo wo

        slice(起始位置,终止位置)
        能够识别负数
        s1="hello world"
        s1.slice(3) //lo world
        s1.slice(3,7) //lo w
        s1.slice(0) //hello world
        s1.slice(-3)//rld
        s1.slice(3,-4) //lo w
        s1.slice(8,3) //"" 错误
        s1.slice(3,7,1)  //lo w
(4)字符串大小写转换
   s1="hello"
   s1.toLowerCase()  小写
   s1.toUpperCase()  大写
 s1="hello"
      console.log( s1.toUpperCase() ); //HELLO

(5)去空格
        s1=" he llo "
        s1.trim()

(6)split 字符串分割
 s1="he,l,lo,wo,r,d"
 s1.split(",")

 console.log(s1.split(","));  //[he,l,lo,wo,r,d]
   s1.split(",",2)   //[he,l]  


(7)字符串的的位置方法
  indexOf()  返回指定字符的第一个位置,如果找不到,返回-1   第二个参数表示查找的起始位置
   
    s1="hello"
    s1.indexOf("e")  //1
    s1.indexOf("o") //4
    s1.indexOf("m")  //-1
    s1.indexOf("o",6) //7  第二个参数表示查找的起始位置
  
    s1.lastIndexOf("o") 
     s1.lastIndexOf("1") 

(8)替换 replace
    [1]返回新字符串
    [2]替换第一个匹配项

    s1="hello"
    s1.replace("e","m")    //hmllo
   
    s1.replace("1","m")   //hemlo  

s2="helloaaaaaaaaaa"   将所有的a替换为m
        rgb=/a/ig
        s3=s2.replace(rgb,"m");
        console.log(s3);          //hellommmmmmmmmm

正则表达式:i-忽略大小写     g-全局查找
rgb=/a/ig
s3=s2.replace(rgb,"m")

二.ASCII码
a  97 ------z  122
A  65------Z  90
0  48-------9  57
回车  13
空格  32


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值