题目
如果第二个参数 bUnicode255For1 === true,则所有字符长度为 1
否则如果字符 Unicode 编码 > 255 则长度为 2
示例1
输入:
'hello world, 牛客', false 输出: 17
编辑
核心代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>获取字符串长度</title> </head> <body> <script>function strLength(s, bUnicode255For1) { var length = s.length; if (!bUnicode255For1) { for (var i in s) { if (s.charCodeAt(i) > 255) { length++; } } } return</script> </body> </html>