zoukankan      html  css  js  c++  java
  • js — 字符串

    字符串

    typeof 用于校验当前变量的数据类型

    var b = 'asd'
    console.log(typeof b); 
    

    1. 拼接字符串

    在Javascript中拼接字符串使用加号(+)操作符,之前我们是拿+来做数字加法运算,但是在这种情况下,它做了一下不同的事情。

    <script>
        var name = 'wusir', age = 28;
        //es6以下版本的字符串拼接
        var str = name + '今年是' + age + '岁了,快要结婚了,娶了个黑姑娘';
        console.log(str);
        //es6的模板字符串: ``反引号
        var str2 = `${name}今年是${age}岁了,快要结婚了,娶了个黑姑娘`;
        console.log(str2);
    </script>
    

    2. 获取字符的方法

    访问字符串中特定字符的方法:

    • charAt() 以单字符字符串的形式返回给定位置的那个字符 (ECMAScript 中没有字符类型)。
    • charCodeAt() 以单字符字符串的形式返回给定位置的那个字符的ASCII编码

    这两个方法都接收一个 参数,即基于 0 的字符位置。

    3. 字符串操作方法(切片)

    1.concat() 用于将一或多个字符串拼接起来, 返回拼接得到的新字符串。

    2.基于子字符串创建新字符串的方法: slice()、substr() 和 substring()

    这三个方法都会返回被操作字符串的一个子字符串,而且也都接受一或两个参数。第一个参数指定字符串的开始位置,第二个参数(在指定的情况下)表示字符串到哪里结束。

    • slice() 和 substring() 的第二个参数指定的是字符串最后一个字符后面的位置。
    • substr() 的第二个参数指定的则是返回的字符个数。如果没有给这些方法传递第二个参数,则将字符串的长度作为结束位置。
    var stringValue = "hello world";
    alert(stringValue.slice(3));//"lo world"
    alert(stringValue.substring(3));//"lo world"
    alert(stringValue.substr(3));//"lo world"
    alert(stringValue.slice(3, 7));//"lo w"
    alert(stringValue.substring(3,7));//"lo w"
    alert(stringValue.substr(3, 7));//"lo worl"
    
    alert(stringValue.slice(-3));//"rld" 
    alert(stringValue.substring(-3));//"hello world"
    alert(stringValue.substr(-3)); //"rld"
    alert(stringValue.slice(3, -4));//"lo w" 
    alert(stringValue.substring(3, -4));//"hel"
    alert(stringValue.substr(3, -4)); //""(空字符串)
    

    4. 字符串位置方法 — 索引

    从字符串中查找子字符串的方法: indexOf() 和 lastIndexOf()

    indexOf() 和 lastIndexOf() 都是从 一个字符串中搜索给定的子字符串,然后返回子字符串的位置(如果没有找到该子字符串,则返回-1)。

    indexOf() 和 lastIndexOf() 的区别:

    • indexOf() 方法从字符串的开头向后搜索子字符串
    • lastIndexOf() 方法 是从字符串的末尾向前搜索子字符串
    var stringValue = "hello world";
    alert(stringValue.indexOf("o"));             //4
    alert(stringValue.lastIndexOf("o"));         //7
    alert(stringValue.indexOf("o", 6));         //7
    alert(stringValue.lastIndexOf("o", 6));     //4
    

    5. trim()方法

    trim()方法 — 删除字符串的前后空格

    var stringValue = "   hello world   ";
    var trimmedStringValue = stringValue.trim();
    alert(stringValue);            //"   hello world   "
    alert(trimmedStringValue);     //"hello world"
    

    6. 字符串大小写转换方法

    ECMAScript 中涉及字符串大小写转换的方法:toUppercase() 、tolowercase()

    var stringValue = "hello world"
    //转大写
    alert(stringValue.toUpperCase()); //"HELLO WORLD"
    //转小写
    alert(stringValue.toLowerCase()); //"hello world"
    
  • 相关阅读:
    触发器实现从TagBlinkLogs往历史表TagLocationHis20125插入一条数据,实现的是在不同的条件下改变相应的状态
    v$sql,V$SQLTExT和v$sqlarea区别与联系
    centos 下增加swap空间大小
    【转载】telnet: connect to address 127.0.0.1: Connection refused
    【原】centos系统命令部分不可用
    [转]linux下的ssh配置
    [原]linux 配置 ssh 等效性
    ORA03113: endoffile on communication channel Process ID: 252 Session ID: 1 Serial number: 3
    【转载】使用rlwrap增强Linux中的sqlplus命令行功能
    [转]详细解说:简单CSS3实现炫酷读者墙
  • 原文地址:https://www.cnblogs.com/yangjie0906/p/11405305.html
Copyright © 2011-2022 走看看