5

是否可以在字符串后插入退格符。如果可能,如何在字符串中插入退格符?

4

3 回答 3

11

退格的转义序列是:

\b

https://social.msdn.microsoft.com/Forums/en-US/cf2e3220-dc8d-4de7-96d3-44dd93a52423/what-c​​haracter-escape-sequences-are-available-in-c?forum=csharpgeneral

C# 定义了以下字符转义序列:

  • \' - 单引号,字符文字需要
  • \" - 双引号,字符串文字需要
  • \\ - 反斜杠
  • \0 – 空
  • \a - 警报
  • \b - 退格
  • \f - 换页
  • \n - 换行
  • \r - 回车
  • \t - 水平制表符
  • \v - 垂直引号
  • \u - 字符的 Unicode 转义序列
  • \U - 代理对的 Unicode 转义序列。
  • \x - Unicode 转义序列类似于“\u”,但长度可变。
于 2013-07-01T15:37:47.067 回答
5

取决于您要达到的目标。要简单地删除最后一个字符,您可以使用它:

string originalString = "This is a long string";
string removeLast = originalString.Substring(0, originalString.Length - 1);

removeLast将给This is a long string

于 2013-07-01T15:37:51.373 回答
2

这将在字符串中插入一个退格

string str = "this is some text";
Console.Write(str);
Console.ReadKey();
str += "\b ";
Console.Write(str);
Console.ReadKey();
//this will make "this is some tex _,cursor placed like so.

如果它像 Belogix 所说的(删除最后一个字符),你可以像 belogix 那样做或其他方式,如:

string str = "this is some text";
Console.WriteLine(str);
Console.ReadKey();

Console.WriteLine(str.Remove(str.Length - 1,1));
Console.ReadKey();

要不就:

string str = "this is some text";
Console.WriteLine(str + "\b ");
于 2013-07-01T16:09:19.217 回答