zoukankan      html  css  js  c++  java
  • 字符串常用方法.html

    <script type="text/javascript">
    //字符串常用方法(不改变原始字符串) <ES5
    var str='hello world'

    //1.charAt() 按照索引查找字符串的内容,并返回
    //语法:字符串.charAt(需要查找的索引 )
    //返回值:对应索引上的字符串(没有就是空字符串)
    var res=str.charAt(4)
    console.log(str) //hello world
    console.log(res) //o


    //2.charCodeAt()
    //语法:字符串.charCodeAt(需要查找的索引 )
    //返回 值 :对应索引位置的字符的编码(十进制)
    var res=str.charCodeAt(0)
    console.log(str) //hello world
    console.log(res) //104


    //3.substring() 截取字符串
    //语法:字符串 .substring(开始位置索引,结束位置索引)包前不包后
    //返回值:截取位置的字符(包前不包后)
    var res=str.substring(0,4)
    console.log(str) //hello world
    console.log(res) //hell


    //4.substr() 截取字符串
    //语法:字符串.aubstr(开始位置索引,多少个)
    //返回值:截取位置的字符
    var res=str.substr(2,5)
    console.log(str) //hello world
    console.log(res) //llo w


    //5.concat() 拼接字符串
    //语法:字符串.concat(需要拼接的字符串)
    //返回值:拼接好的字符串
    var res=str.concat('您好 世界' ,'linux access') //=str+'您好 世界'
    console.log(str) //hello world
    console.log(res) //hello world您好 世界linux access


    //6.slice() 截取字符串 与 substring() 方法一样
    //语法:字符串.slice(开始位置索引,结束位置索引)包前不包后
    //返回值:截取的字符串
    var res=str.slice(0,4)
    console.log(str) //hello world
    console.log(res) //hell


    //7.toUpperCase() 字符串转大写
    //语法:字符串.toUpperCase()
    //返回值:字符串大写内容
    var res=str.toUpperCase()
    console.log(str) //hello world
    console.log(res) //HELLO WORLD

    var str3='123456'
    var res=str3.toUpperCase()
    console.log(str3) //123456
    console.log(res) //123456

    var str2='HELLO WORLD'
    //8.toLowerCase() 字符串转小写
    //语法:字符串.toLowerCase()
    //返回值:字符串小写内容
    var res=str2.toLowerCase()
    console.log(str2) //HELLO WORLD
    console.log(res) //hello world


    var str4='1-2-3-4-5-6'
    //9.split() 字符串切割,按照我们给的条件,把字符串分成几截,然后在一个数组中返回
    //语法:字符串.split(用什么进行切割)
    //返回值:返回一个数组
    var res=str4.split('-') //传空字符串时,是按照一位一位的切
    console.log(str4) //1-2-3-4-5-6
    console.log(res) //(6) ["1", "2", "3", "4", "5", "6"]


    var str5='你好,世界,你好,世界,你好,世界'
    //10.replace() 替换字符串中的内容 (作文本查询)
    //语法:字符串.replace(把什么,替换成什么)
    var res=str5.replace('你好','hello')
    console.log(res) //hello,世界,你好,世界,你好,世界
    console.log(str5) //你好,世界,你好,世界,你好,世界 (一次只能替换一个)


    //字符串常用方法 ES5

    //1.indexOf() 查找某一个字符的索引位置
    //第一种方式

    //语法:字符串.indeOf(字符片段)
    //返回值:
    // 如果找到对应字符片段,那么返回字符片段开始位置索引
    // 如果没有找到对应字符片段。则返回-1
    var str6='hello world'
    var res=str6.indexOf('lo')
    console.log(str6) //hello world
    console.log(res) //3

    //第二种方式
    //语法:字符串.indeOf(字符片段(只能是一个字符,否则返回-1),从哪个索引位置开始查找)
    var res=str6.indexOf('l',5)
    console.log(str6) //hello world
    console.log(res) //9

    </script>

  • 相关阅读:
    浅析ES6中的iterator
    nodejs中的异步回调机制
    用好js与nodejs中的try...catch
    vscode设置html默认浏览器
    nodejs中相互引用(循环引用)的模块分析
    ES6—带默认值的函数参数及其作用域
    函数声明与函数表达式的区别
    let块级引起的闭包思考
    进程与线程
    angular(^4)-监控表格按键行为的问题
  • 原文地址:https://www.cnblogs.com/d534/p/12711989.html
Copyright © 2011-2022 走看看