从字符串中消除超出范围的 ASCII 字符
Eliminating out-of-range ASCII characters from string
使用 JavaScript,我想减少字符串以仅包含 ASCII 65-90 内的字符(A-Z 中的大写字母)。
我的函数首先将字符串转换为全大写。接下来消除空格,最后将字母转换为 ASCII 小数。
我只想要字母 A-Z (ASCII 65-90),没有别的。但是,如果字符串确实包含一个或多个不需要的字符怎么办?有没有办法从字符串中消除字符 <
ASCII 65 和 >
ASCII 90 的所有实例?
您可以使用简单的 reduce 调用:
const input = "asddgAeBcc6$$Cz>>,,";
const output = Array.prototype.reduce.call(input, (res, c) => res + ((c >= 'A' && c <= 'Z') ? c: ""), "");
console.log({ input, output })
使用 JavaScript,我想减少字符串以仅包含 ASCII 65-90 内的字符(A-Z 中的大写字母)。
我的函数首先将字符串转换为全大写。接下来消除空格,最后将字母转换为 ASCII 小数。
我只想要字母 A-Z (ASCII 65-90),没有别的。但是,如果字符串确实包含一个或多个不需要的字符怎么办?有没有办法从字符串中消除字符 <
ASCII 65 和 >
ASCII 90 的所有实例?
您可以使用简单的 reduce 调用:
const input = "asddgAeBcc6$$Cz>>,,";
const output = Array.prototype.reduce.call(input, (res, c) => res + ((c >= 'A' && c <= 'Z') ? c: ""), "");
console.log({ input, output })