zoukankan      html  css  js  c++  java
  • [js] charAt()、charCodeAt()、fromCharCode()

    String.prototype.charAt()

    str.charAt(index)

     返回字符串中指定位置的字符。

     字符串中的字符从左向右索引,第一个字符的索引值为 0,最后一个字符(假设该字符位于字符串 stringName 中)的索引值为 stringName.length - 1。

    如果指定的 index 值超出了该范围,则返回一个空字符串。

    复制代码
                var anyString = "Brave new world";
                console.log("The character at index 0   is '" + anyString.charAt(0) + "'");
                console.log("The character at index 1   is '" + anyString.charAt(1) + "'");
                console.log("The character at index 2   is '" + anyString.charAt(2) + "'");
                console.log("The character at index 3   is '" + anyString.charAt(3) + "'");
                console.log("The character at index 4   is '" + anyString.charAt(4) + "'");
                console.log("The character at index 999 is '" + anyString.charAt(999) + "'");
    //            The character at index 0 is 'B'
    //            The character at index 1 is 'r'
    //            The character at index 2 is 'a'
    //            The character at index 3 is 'v'
    //            The character at index 4 is 'e'
    //            The character at index 999 is ''
    复制代码

    String.prototype.charCodeAt()

    str.charCodeAt(index)

    返回指定索引处字符的 Unicode 数值(Unicode 编码单元 > 0x10000 的除外)。

    Unicode 编码单元(code points)的范围从 0 到 1,114,111。开头的 128 个 Unicode 编码单元和 ASCII 字符编码一样。
    如果指定的 index 小于 0 或大于字符串的长度,则 charCodeAt 返回 NaN。

    大于255为中文

    一字节有8位2进制数,从0开始,2的8次方就是255;如果大于255,就表示占用了2个字节  

    "ABC".charCodeAt(0) // returns 65
    复制代码
                //求一个字符串的字节长度
                function GetBytes(str) {
                    var len = str.length;
                    var bytes = len;
                    for (var i = 0; i < len; i++) {
                        //console.log(str[i],str.charCodeAt(i));
                        if (str.charCodeAt(i) > 255) bytes++;
                    }
                    return bytes;
                }
                console.log(GetBytes("你好,as"));
    复制代码

    String.fromCharCode()

    String.fromCharCode(num1, ..., numN)

    String.fromCharCode() 静态方法根据指定的 Unicode 编码中的序号值来返回一个字符串。

            String.fromCharCode(65,66,67)
            //"ABC"

    #字母 <-> ASCII <-> 十六进制

    复制代码
                var Converter = (function() {
                    var $ = {};
                    $.toAscii = function(hex) {
                        var temp = '';
                        for (var i = 0; i < hex.length; i = i + 2) {
                            temp += String.fromCharCode(parseInt(hex.slice(i, i + 2), 16));
                        }
                        return temp;
                    }
                    $.toHex = function(ascii) {
                        var temp = '';
                        for (var i = 0; i < ascii.length; i++) {
                            temp += ascii.charCodeAt(i).toString(16);
                        }
                        return temp;
                    }
                    return $;
                })();
  • 相关阅读:
    JMeter接口测试系列:Jmeter+jenkins+ant 的自动化构建
    BZOJ1434:[ZJOI2009]染色游戏(博弈论)
    BZOJ4241:历史研究(回滚莫队)
    BZOJ2281:[SDOI2011]黑白棋(博弈论,组合数学,DP)
    BZOJ1188:[HNOI2007]分裂游戏(博弈论)
    BZOJ1022:[SHOI2008]小约翰的游戏John(博弈论)
    BZOJ1406:[AHOI2007]密码箱(数论)
    BZOJ3270:博物馆(高斯消元)
    CF993E:Nikita and Order Statistics(FFT)
    HDU4609:3-idiots(FFT)
  • 原文地址:https://www.cnblogs.com/qingqinglanlan/p/7398087.html
Copyright © 2011-2022 走看看