字符串的属性和方法

一、字符串操作

1.字符串的创建

    var s1="hello"
    var s2=new String("hello")

2.属性

s1.length 获取字符串的长度

3.方法

(1)获取字符
charAt() 返回给定的位置的字符

  charCodeAt()  返回字符编码



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

(2) 拼接字符串

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

(3) 子字符串
s1=“hello world”
substr(起始位置,长度):从起始位置截至指定长度的字符串
如起始位置为负数,则倒数
console.log(s1.substr(3)) //lo world
console.log(s1.substr(3,6)) //lo wor
console.log(s1.substr(0)) //hello world
console.log(s1.substr(-3)) //rld
console.log(s1.substr(3,-4)) //“”
console.log(s1.substr(8,3)) //rld

  substring(起始位置,终止位置):起始包含,终止不包含
            遇到负数,会自动转换为0
            起始如大于终止则自动调换
    console.log(s1.substring(3))    //lo world
    console.log(s1.substring(3,6))  //lo 
    console.log(s1.substring(0))    //hello world
    console.log(s1.substring(-3))   //hello world
    console.log(s1.substring(3,-4)) //hel
    console.log(s1.substring(8,3))  //lo wo

  slice(起始位置,终止位置)
        识别负数
    console.log(s1.slice(3))    //lo world
    console.log(s1.slice(3,7))  //lo w
    console.log(s1.slice(0))    //hello world
    console.log(s1.slice(-3))   //rld
    console.log(s1.slice(3,-4)) //lo w
    console.log(s1.slice(8,3))  //""

(4) 字符串大小写转换
s1=“hello”
s1.toLowerCase() 小写
s1.toUpperCase() 大写

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

(6) split 字符串分割
s1=“he,l,lo,wo,r,l,d”
s1.split(“,”) //[‘he’, ‘l’, ‘lo’, ‘wo’, ‘r’, ‘l’, ‘d’]
s1.split(“,”,2) //[‘he’, ‘l’]

  s2="heollolw"
  s2.split("ol")    //['he', 'l', 'w']

(7) 字符串位置方法
indexOf() 返回指定字符串中的第一个位置,如找不到返回-1
第二个参数表示查找的起始位置
lastIdexOf() 从后向前查找

    s1="hello world"
    console.log( s1.indexOf("e")) //1
    console.log( s1.indexOf("o")) //4 
    console.log( s1.indexOf("m")) //-1
    console.log( s1.indexOf("o",6)) //7

    console.log( s1.lastIndexOf("o")) //7
    console.log( s1.lastIndexOf("l")) //9

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

    s1="hello"
    console.log(s1.replace("e","m")) //hmllo
    console.log(s1.replace("l","m")) //hemlo

    s1="hello"
    while(s1.indexOf("l")!=-1){
        s1=s1.replace("l","m")
    }
    console.log(s1)

    s2="heabAcabca"
    //正则表达式,i忽略大小写,g全局查找
    reg=/a/ig
    s3=s2.replace(reg,"m")
    console.log(s3)       //hembmcmbcm

4、字符串删除

var s1=“hello”
s1.replace(“e”,“”)

(2)删除字符串的第一位
删除字符串的最后一位
substr
// var a=a1.substr(0,1)
// var a=a1.substr(0,a1.length-2)

(3)截取字符串的开始和结束
substring
// function f1(a1){
// var a=a1.substring(0,1)
// var b=a1.substring(a1.length-1)
// alert(“起始位置是”+a+“终止位置是”+b)
// }
// f1(“asdfg”)

(4)删除指定的字符
s1=helloworld"
split
s1=“helloworld”
s2=s1.split(“o”)
s2=s2.join(“”)

5、字符串的比较

(1)localCompare:调用本地操作系统的排序规则
v1.localCompare(v2)

    v1=v2,返回0
    v1排在v2之前返回-1
    v1排在v2之后返回1


    s1="abc"
    s2="edf"
    //s3="abc,edf"
    s1.localCompare(s2)    //-1
    a="yellow".localeCompare("yellow")   //0
    b="yellow".localeCompare("blue")     //1
    c="yellow".localeCompare("zoo")      //-1


    中文比较--按拼音,第一个相同比下个
    console.log("北京".localeCompare("阿城"))   //1
    console.log("北京".localeCompare("上海"))   //-1
    console.log("杭州".localeCompare("河北"))   //-1

     a1=["哈尔滨","北京","上海","杭州","深圳"]   //排序(升序)
    a1.sort((a,b)=>{return a.localeCompare(b)})
    //=>箭头函数
    //srot() 数组排序函数
    //在sort()里添加箭头函数
    console.log(a1)

(2)alert(“a”>“A”) true 按AscII码编排

6、字符串编码

(1)charCodeA
s1=“a”
s1.charCodeAt(0) //97
s1=“海”
s1.charCodeAt(0) //28023

(2)fromCharCode():根据AscII码或Unicode码的10进制码进行解析
console.log(String.fromCharCode(65)) //A
console.log(String.fromCharCode(28023)) //海

练习:用户输入必须为中文
4E00-9FCF

(3)escape --输出unicoed码
unescape --根据unicode码输出字符
s1=“海”
escape(s1) //\u6d77
unescape(\u6d77) //海

 encodeURI(s1)   //%E6%B5%B7   加密
 decodeURI("%E6%B5%B7")   //   解密
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秃头程序员-疯子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值