请解释这种意外的\ b(退格)行为 [英] Please explain this unexpected \b (backspace) behavior

查看:82
本文介绍了请解释这种意外的\ b(退格)行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预期:

>>> print "I print\b\b Backspace\b\b!"
I pri Backspa!
>>> print "I print\b\b Backspace\b\b\b!"
I pri Backsp!

已观察:

>>> print "I print\b\b Backspace\b\b!"
I pri Backspa!e
>>> print "I print\b\b Backspace\b\b\b!"
I pri Backsp!ce

为什么不删除'e'和'ce'和'!'插入了吗?

Why does is 'e' and 'ce' not erased and '!' inserted?

推荐答案

\ b 也称为退格,将光标向后移动1.

The \b also termed as back space moves back the cursor by 1.

退格键不会删除任何内容,它会将光标移到左侧,并且随后的内容将其覆盖.

The backspace doesn't delete anything, it moves the cursor to the left and it gets covered up by what you write afterwards.

让我们通过您的示例来理解这一点:

Let's understand this with your example:

"I print\b\b Backspace\b\b!"    # original string passed to "print"
#     ^^ * *        ^^ * *
#     12 1 2        34 3 4

  • 执行 * 1 * 2 后,光标移至 ^ 1 .因此, ^ 1 被空格''替换,而 ^ 2 B 替换(之后的字符\ b )

    • After executing *1 and *2, cursor comes at ^1. Hence, ^1 is replaced by space ' ' and ^2 replaced by B (characters following \b)

      在执行 * 3 * 4 之后,光标位于 ^ 3 处,并被.由于之后没有任何内容,因此 ^ 4 保持原样,否则将被下一个字符替换.

      After executing *3, and *4, cursor comes at ^3 and is replaced by !. Since there was nothing after !, ^4 remains as it is, else would have be replaced by the next character.

      因此,打印在屏幕上的结果内容如下:

      Hence the resultant content that is printed on the screen is as:

      I pri Backspa!e
      

      这篇关于请解释这种意外的\ b(退格)行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆