How to remove numbers from a string?

前端 未结 7 1687
遇见更好的自我
遇见更好的自我 2020-12-12 19:56

I want to remove numbers from a string:

questionText = \"1 ding ?\"

I want to replace the number 1 number and the question mar

7条回答
  •  半阙折子戏
    2020-12-12 20:42

    This can be done without regex which is more efficient:

    var questionText = "1 ding ?"
    var index = 0;
    var num = "";
    do
    {
        num += questionText[index];
    } while (questionText[++index] >= "0" && questionText[index] <= "9");
    questionText = questionText.substring(num.length);
    

    And as a bonus, it also stores the number, which may be useful to some people.

提交回复
热议问题