JavaScript内置对象学习笔记二——String字符串对象

1.String对象

        String对象用于处理已有的字符串

        字符串可以使用单引号或双引号

2.在字符串中查找字符串:indexof()

<script>
        var str = "hello world";
        //indexof() 返回已存在字符串的位置
        document.write(str.indexOf("hello"));//这里返回为:0
        document.write(str.indexOf(" "));//这里返回为:5
        document.write(str.indexOf("world"));//这里返回为:6
</script>

3.内容匹配:match()

<script>
        var str = "hello world";
        //match() 匹配字符串位置,存在返回匹配值,不存在返回null
        document.write(str.match("world"));//这里返回为:world
        document.write(str.match("world1"));//这里返回为:null
</script>

4.替换内容:replace()

<script>
        var str = "hello world";
        //replace() 替换原有的字符串并返回替换后的字符串
        document.write(str.replace("World","csdn"));//这里返回:hello csdn
</script>

5.字符串大小写的转换:toUpperCase() / toLowerCase()

<script>
        var str = "hello world";
        //toUpperCase()将字符串全部转化成大写
        document.write(str.toUpperCase());//这里返回:HELLO WORLD
        //toUpperCase()将字符串全部转化成小写
        document.write(str.toLowerCase());//这里返回:hello world
</script>

6.字符串转化为数组:strong>sqlit()

<script>
        var str = "hello world";
        var str1 = "hello,csdn";
        //split() 对字符串的分隔标识符进行选择,str是" "空格,str1是","逗号
        var s = str1.split(",");
        document.write(s[1]);//这里返回:csdn
</script>

7.字符串大部分的属性和方法:

        1.属性:length、prototype、constructor

        2.方法:charAt()、charCodeAt()、concat()、fromCharCode()、indexOf()、lastIndexOf()、match()、replace()、search()、substring()、substr()、valueOf()、toLowerCase()、toUpperCase()、split()

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值