zoukankan      html  css  js  c++  java
  • JS字符串之操作方法

    操作方法

    1. concat() 拼接字符串

    拼接一个或多个字符串,返回新字符串。(隐式类型转换)

    也可以用加号轻松实现字符串的拼接。

    var str = "Hello world!";
    // concat() 拼接字符串
    console.log(str.concat(123)); // 返回新数组副本
    // 可以拼接任意多个参数
    console.log(str.concat(123,"1545sdsdd",true));
    // + 加号也可以实现字符串的拼接
    console.log(str + 123);
    

    2. slice(start, end)/substring(start, end)、substr(start, length) 切片方法

    当有两个参数时,slice()和substring()的用法一样,顾头不顾尾。

    substring()方法不接受负数。

    substr()的第二个参数表示操作的字符个数。

    // slice(start, end)/substring(start, end)、substr(start, length) 切片方法
    // 输入一个参数时
    console.log(str.slice(1)); // ello world!
    console.log(str.substring(1)); // ello world!
    console.log(str.substr(1)); // ello world!
    
    console.log(str.slice(-3)); // slice(12-3) => slice(9) => ld!
    console.log(str.substring(-2)); // 不接受负数!这里会把负数转换成0,所以会全部返回 => Hello world!
    console.log(str.substr(-3)); // ld!
    
    // slice() substring() 顾头不顾尾   
    console.log(str.slice(1,7));  // ello w
    console.log(str.substring(1,7)); // ello w
    console.log(str.slice(-8, -3)); // slice(12-8,12-3) => slice(4,9) => o wor
    // substr() 第二个参数表示字符长度
    console.log(str.substr(0,5)); // Hello
    

    3. indexOf()、lastIndexOf() 位置方法

    在字符串中查找给定字符的位置,返回该位置。找不到就返回-1。

    // indexOf() lastIndexOf() 
    console.log(str.indexOf('o')); // 4
    console.log(str.indexOf('o', 5)); // 7
    console.log(str.lastIndexOf('o')); // 7
    console.log(str.lastIndexOf('o', 6)); // 4
    console.log(str.indexOf(123)); // -1
    console.log(str.lastIndexOf(123)); // -1
    

    4. trim() 去除前后方的空格

    创建一个字符串的副本,删除字符串前置及后缀的所有空格,然后返回结果。

    // trim() 不会影响原数组 返回新数组的副本
    var str2 = "   Hello Wor ld ! ";
    console.log(str2.trim());
    console.log(str2);
    

    5. toLowerCase() toUpperCase() 转换大小写

    toLowerCase() 将给定字符串转换成小写
    toUpperCase() 将给定字符串转换成大写

    // toLowerCase() toUpperCase()
    var str3 = "Hello World!";
    console.log(str3.toLowerCase()); // hello world!
    console.log(str3.toUpperCase()); // HELLO WORLD!
    
  • 相关阅读:
    Discovery Scanning
    Openvas
    Common Vulnerability Scoring System CVSS
    NIagara Workbench ( 温度控制)
    Nikto and whatweb
    Jace Config
    Active information gathering-services enumeration
    Intsall The Nessus in you kali linux
    Source Code Review
    Niagara workbench (Basic )
  • 原文地址:https://www.cnblogs.com/buildnewhomeland/p/12421102.html
Copyright © 2011-2022 走看看