如果以下字符不需要转义,转义字符应该怎么做? [英] What should an escape character do if the following character doesn't need escaping?

查看:513
本文介绍了如果以下字符不需要转义,转义字符应该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的应用程序有一个可以输入字符串的字段。

An application I'm working on has a field where a string can be entered.

字符串中的特殊字符会导致不同的东西插入字符串评估,但是这些特殊字符之前可以有一个转义字符(反斜杠),这个字符会导致特殊字符从字面上输出,而不是其特殊意义。

Special characters in the string cause different things to be inserted when the string is evaluated, but these special characters can be preceded by an escape character (a backslash) which cause the special character to be output literally rather than its special meaning.

想想类似于正则表达式:匹配任何字符,但 \。匹配一个点。

Think of it as similar to a regular expression: . matches any character but \. matches a dot.

当转义字符后跟一个不需要转义的字符时,最直观的事情是什么?例如,更有意义的是:

What is the most intuitive thing to happen when the escape character is followed by a character which doesn't need escaping? For example, would it make more sense that:


  1. 转义字符转义自己的文字: \\ \\ f 成为 f (a escaped f)

  2. 转义字符不是转义字符,除非它后面是一个特殊字符: \f 保持为 \f

  3. 错误!抛出一个例外,因为无法逃避任何转义字符。

  1. The escape character "escapes" the literal as itself: \f becomes f ("an escaped f")
  2. The escape character isn't an escape character unless it's followed by a special character: \f remains as \f
  3. Error! Throw an exception because the it's invalid to have an escape character not escaping anything

所有这些都是可能的,在我看来是合理的。但是哪个更有意义,哪些在其他语言中最常见?

All of these are possible and in my opinion justifiable. But which makes more sense, and which is already most common in other languages?

推荐答案

我会投票支持第一个解决方案。它使得形成字符串变得容易:如果你不知道一个角色在你的上下文中是否具有特殊的意义,那就逃避它了。不会伤害。

I'd vote for the first solution. It makes forming strings easy: If you don't know if a character has a special meaning in your context, just escape it. It won't hurt.

这篇关于如果以下字符不需要转义,转义字符应该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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