Skip to content

编写一个方法,求一个字符串的字节长度 #3

@hubvue

Description

@hubvue

字符串中,英文字符站一个字节,中文字符站两个字节。

原理:待补充!

/**
 * 
 * 编写一个方法,求一个字符串的字节长度
 * 
 */

const byteLen = str => {
    let len = str.length;
    for(let s of str) {
        if(s.charCodeAt() >255){
            len ++;
        }
    }
    return len;
} 
const result = byteLen('fdsafd网');
console.log(result);

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions