【问题标题】:Format string and deleting special characters using ASCII code doesn't work (JavaScript)使用 ASCII 代码格式化字符串和删除特殊字符不起作用(JavaScript)
【发布时间】:2021-07-06 16:28:22
【问题描述】:

我正在尝试使用 ASCII 代码格式化任何给定的字符串以供参考。格式必须删除除数字、“-”或“_”和空格之外的任何特殊字符。代码如下:

 function FormatString(sentence) {
        result = new String();
        j = 0;
        sentence = sentence.toUpperCase();
        i = 0;

        while(i < sentence.length) {
            if (
                (sentence[i]>=65 && sentence[i]<=90) ||
                (sentence[i]>=48 && sentence[j]<=57) ||
                sentence[i]==32 || sentence[i]==45
            ) {
                sentence[j] = result[i];
                j = j + 1;
            }
            i = i + 1;
        }
        return result;
    }

然后调用函数

console.log(FormatString('No running in the hallways!!!'))

输出应该是“不要在走廊里跑步”

【问题讨论】:

  • 起初,sentence 仍然包含字符,而不是代码点,而且字符串是不可变的,您不能更改字符串中的单个字符。看看RegExps,它们是您完成任务所需要的。
  • @Teemu 我想我明白你的意思,那么,我应该改变或做什么?
  • sentence[j] = result[i]; 这似乎倒退了。 result += sentence[j]?如果您有sentence[j]&lt;=57,也可以在其中sentence[i]&lt;=57 应该是。
  • @James 它仍然返回一个空字符串,即使很难我将格式化的句子分配给结果变量

标签: javascript string format frontend ascii


【解决方案1】:

假设使用Regular Expressions 是解决您的问题的最佳方法,我假设您想使用 ASCII 比较是有原因的。

以下解决方案有效:

function FormatString (sentence) {
    let result = "";

    const uppercaseSentence = sentence.toUpperCase();

    for (let i=0; i<uppercaseSentence.length; i++) {
        let cc = uppercaseSentence.charCodeAt(i);
        if ((cc>=65 && cc<=90) || (cc>=48 && cc<=57) || cc==32 || cc==45) {
            result += sentence[i];
        }
    }

    return result;
}

我在你的功能下方报告并评论它的问题:

function FormatString(sentence) {
    
    // you are declaring a global variable here.
    // let result = ... or var result = ... makes it local
    // In javascript you can use an string literal `""` in place of new String()
    result = new String();
    
    // again a global variable
    j = 0;
    
    // you are chaning the passed parameter to an uppercase string
    // you lost the original string now
    sentence = sentence.toUpperCase();
    
    // again a global variable
    i = 0;

    // nothing wron with the while loop, only the for loop is more concise in
    // this case
    while(i < sentence.length) {
        
        if (
            // sentence[i] is a streing with length 1 and it returns false if
            // compared with a number. Use instead sentence.charCodeAt(i) which
            // returns the ASCII code of the i-th character
            (sentence[i]>=65 && sentence[i]<=90) ||
                                // there is a j here in place of an i
            (sentence[i]>=48 && sentence[j]<=57) ||
            sentence[i]==32 || sentence[i]==45
        ) {
            // you can't assign single characters in javascript
            // what you want to do is result += sentence[i],
            // but remember that sentence is now all uppercase and so will be 
            // result!
            sentence[j] = result[i];
            j = j + 1;
        }
        i = i + 1;
    }
    
    return result;
}

【讨论】:

  • 非常感谢!这很有帮助,现在我理解了这个练习,感谢您对原始功能的奖金解释!
【解决方案2】:

也许string.replace 与正则表达式将帮助您的需求:

const regex = /[-_]/g
"afasf-afsaf_fafa".replace(regex,''); //"afasfafsaffafa"

【讨论】:

  • 感谢您的回复,但“_”和“-”应该保持不变,我要格式化的是其他特殊字符,我需要使用给出的代码,因为这是一个练习
  • 对不起,我误解了你的问题
猜你喜欢
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
  • 2011-09-27
  • 1970-01-01
相关资源
最近更新 更多