A method of operating a string js

All methods string, the string itself is not modified (non-variable length string)

  1. Firstly, two ways to create a string

    var s = 'zs' ;;;
    fact var s = new String ( 'zs ') ;;;

  2. 1. The position (index) acquired the character: charAt (index), returns the character index is the index, from 0 ... / understanding: charCodeAt (index) to obtain the specified character code ascii

    var str = 'abcdefg';
    function cAt(str) {
        for(var i = 0; i < str.length; i++) {
            var s = str.charAt(i);
            console.log(s);
        }
    }
    cAt(str);
  1. the concat (); connecting two or more characters in the form str.a (concat (b, c ...))
    var str1 = 'abcd';
    var str2 = 'efgh';
    console.log(str1.concat(str2));
    
    输出结果为 : abcdefgh
  1. substr (start, length); string from which the start interception, the interception of several
    var str = 'abcdefgh';
    console.log(str.substr(1, 1));
    
    输出的结果为 : b 意为从索引为 1 的地方开始 , 截取的长度为 1
  1. indexOf ( 'a'); find a character position
    var str = 'abcdefg';
    console.log(str.indexOf(2));
    结果为 -1 这个方法,如果能在字符串中找的到返回的结果就是 1 ,如果找不到,返回的结果就是-1
    console.log(str.indexOf('b'));
    返回的结果就是 1
  1. Alternatively replice replace ( 'a', 'b'), to replace a, b replace
    var str = 'abcdefgaaaa';
        while (str.indexOf('a') !== -1) {
           str = str.replace('a', '!');
      }
    console.log(str);
  1. Turn uppercase
    var str = 'get-Element-By-Id';
    console.log(str.toUpperCase());
    返回结果为 :get-element-by-id
  1. Small letter
     var str = 'get-Element-By-Id';
            // console.log(str.toUpperCase());
            var arr = str.split('-');
            var a = arr[0];
            for (var i = 1; i < arr.length; i++) {
                a += arr[i].charAt(0).toLowerCase() + arr[i].slice(1);
            }
     console.log(a)
    返回结果为 :getelementbyid

Common method will these strings, here only some basic operations, because there is no difficulty
in this attach my QQ: 2489757828 a problem, then we can explore with
my private blog: [Lida Xuan] ( HTTPS: / /webldx.github .

Guess you like

Origin blog.csdn.net/weixin_43553701/article/details/93385290