[js] charAt()、charCodeAt()、fromCharCode()

本文介绍了JavaScript中字符串操作的基础方法,包括charAt()用于获取指定位置的字符,charCodeAt()用于获取字符的Unicode数值,并展示了如何计算字符串的字节长度。同时,还介绍了fromCharCode()方法,该方法根据Unicode序号值返回对应的字符串。

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 $;
            })();

转载于:https://www.cnblogs.com/qingqinglanlan/p/7398087.html

// query_parameter_encrypt.js const CryptoJS = require("crypto-js"); s = { "payload": { "sort": 1, "start": 0, "limit": 20 } } function _u_e(e) { if (null == e) return null; e = e.replace(/\r\n/g, "\n"); for (var t = "", n = 0; n < e.length; n++) { var r = e.charCodeAt(n); r < 128 ? t += String.fromCharCode(r) : r > 127 && r < 2048 ? (t += String.fromCharCode(r >> 6 | 192), t += String.fromCharCode(63 & r | 128)) : (t += String.fromCharCode(r >> 12 | 224), t += String.fromCharCode(r >> 6 & 63 | 128), t += String.fromCharCode(63 & r | 128)) } return t } var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" , _p = "W5D80NFZHAYB8EUI2T649RT2MNRMVE2O"; function e1(e) { if (null == e) return null; for (var t, n, r, o, i, a, u, c = "", l = 0; l < e.length;) o = (t = e.charCodeAt(l++)) >> 2, i = (3 & t) << 4 | (n = e.charCodeAt(l++)) >> 4, a = (15 & n) << 2 | (r = e.charCodeAt(l++)) >> 6, u = 63 & r, isNaN(n) ? a = u = 64 : isNaN(r) && (u = 64), c = c + _keyStr.charAt(o) + _keyStr.charAt(i) + _keyStr.charAt(a) + _keyStr.charAt(u); return c } function e2(e) { if (null == (e = _u_e(e))) return null; for (var t = "", n = 0; n < e.length; n++) { var r = _p.charCodeAt(n % _p.length); t += String.fromCharCode(e.charCodeAt(n) ^ r) } return t } function sig(e) { return CryptoJS.MD5(e + _p).toString().toUpperCase(); } function generate(s) { var payload = e1(e2(JSON.stringify(s.payload))), sign = sig(payload); return [payload, sign]; } console.log(generate(s)); 给每行代码添加中文注释
最新发布
09-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值