带有退格键的 JavaScript 连接字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11891653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:41:06  来源:igfitidea点击:

JavaScript concat string with backspace

javascriptstringconcatenationbackspace

提问by riship89

I have a function f similar to

我有一个类似于 f 的函数

function f(str){
    alert("abc"+str);
}

Now, I want to use JavaScript special charecter "\b" in such a way that I can choose if I want to display the hardcoded string "abc" or not. For example,

现在,我想以这样的方式使用 JavaScript 特殊字符“\b”,以便我可以选择是否要显示硬编码字符串“abc”。例如,

f("\b\b"+"yz"); //should output "ayz"

I tried the same, but it does not work. In other words, I want to concat a string with a backspace character so that I can remove last characters from the string.

我试过同样的,但它不起作用。换句话说,我想用退格字符连接一个字符串,以便我可以从字符串中删除最后一个字符。

Can we do this in JavaScript?

我们可以在 JavaScript 中做到这一点吗?

EDITThe real code is too much big (its a HUGE 1 liner that concats many many strings). To map that in above example, we cannot edit the function f, so do whatever you want from outside function f.

编辑真正的代码太大了(它是一个巨大的 1 行,它连接了许多字符串)。为了在上面的例子中映射它,我们不能编辑函数 f,所以从函数 f 外部做任何你想做的事情。

采纳答案by Kendall Frey

The problem comes from the fact that \bis just another character in the ASCII code. The special behaviour is only when implemented by some string reader, for example, a text terminal.

问题来自这样一个事实,\b即 ASCII 代码中的另一个字符。特殊行为仅在由某些字符串阅读器(例如,文本终端)实现时才会出现。

You will need to implement the backspace behaviour yourself.

您需要自己实现退格行为。

function RemoveBackspaces(str)
{
    while (str.indexOf("\b") != -1)
    {
        str = str.replace(/.?\x08/, ""); // 0x08 is the ASCII code for \b
    }
    return str;
}

Example: http://jsfiddle.net/kendfrey/sELDv/

示例:http: //jsfiddle.net/kendfrey/sELDv/

Use it like this:

像这样使用它:

var str = RemoveBackspaces(f("\b\byz")); // returns "ayz"

回答by DGH

EDIT: I realized this may not be what the OP was looking for, but it is definitely the easier way to remove characters from the end of a string in most cases.

编辑:我意识到这可能不是 OP 正在寻找的内容,但在大多数情况下,这绝对是从字符串末尾删除字符的更简单方法。

You should probably just use string.substringor string.substr, both of which return some portion of string. You can get the substring from 0 to the string's length minus 2, then concatenate that with "yz" or whatever.

您可能应该只使用string.substringor string.substr,它们都返回字符串的一部分。您可以获得从 0 到字符串长度减 2 的子字符串,然后将其与“yz”或其他内容连接起来。

回答by Wesley

Interesting question. I first checked some assumptions about \b in JS.

有趣的问题。我首先检查了 JS 中关于 \b 的一些假设。

If you try this:

如果你试试这个:

console.log('abc\b\byz');

console.log('abc\b\byz');

You get the same answer of 'abcyz'.

您会得到与 'abcyz' 相同的答案。

This means, it is not a function of concatentation, but a fundamental error in the approach.

这意味着,它不是连接的函数,而是方法中的一个基本错误。

I would modify your approach to use SubString, then to take the index of \b and slice out the previous character.

我会修改您的方法以使用 SubString,然后获取 \b 的索引并切出前一个字符。

回答by Phillip Schmidt

Something like this:

像这样的东西:

function f(str, abc){
   if(!abc) abc = "abc";
   if (str.indexOf("\b") != "undefined")
   {
       abc = abc.slice(0,-1);
       str = str.replace("\b","");
       f(str, abc);
   }
   else alert(abc+str);
}

and as an added bonus you get to use recursion!

作为额外的奖励,您可以使用递归!

note that this is a little slower than doing it this way:

请注意,这比这样做要慢一点:

function f(str){
    var count = 0;
    var abc = "abc";
    for(var i = 0; i < str.length; i++)
    { 
       if(str[i] = "\b") //at least i think its treated as one character...
       count++;
    }
    abc = abc.slice(0, count * -1);
    alert(abc+str);
}