为什么 Java 没有办法指定未转义的字符串文字?

发布于 2024-07-14 07:10:43 字数 258 浏览 9 评论 0原文

在C# 中,如果您希望字符串按字面意思处理,即忽略转义字符,您可以使用:

string myString = @"sadasd/asdaljsdl";

但是在Java 中没有等效的方法。 Java 没有包含类似的东西有什么原因吗?

编辑:

在查看了一些答案并思考之后,我真正要问的是:
是否有任何令人信服的理由反对将此语法添加到 Java 中? 有些负面影响,我只是没有看到?

In C#, if you want a String to be taken literally, i.e. ignore escape characters, you can use:

string myString = @"sadasd/asdaljsdl";

However there is no equivalent in Java. Is there any reason Java has not included something similar?

Edit:

After reviewing some answers and thinking about it, what I'm really asking is:
Is there any compelling argument against adding this syntax to Java? Some negative to it, that I'm just not seeing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

一向肩并 2024-07-21 07:10:43

Java 一直给我留下深刻的印象,因为它是一种极简主义语言 - 我想,由于逐字字符串不是必需的(例如属性),所以它们没有被包含在内。

例如,在 C# 中,有许多快速方法可以执行属性

public int Foo { get; set; }

和逐字字符串之类的操作:

String bar = @"some
string";

Java 倾向于尽可能避免语法糖。 如果你想要一个字段的 getter 和 setter,你必须这样做:

private int foo;

public int getFoo() { return this.foo; }
public int setFoo(int foo) { this.foo = foo; }

并且必须对字符串进行转义:

String bar = "some\nstring";

我认为这是因为在很多方面 C# 和 Java 有不同的设计目标。 C# 发展迅速,许多功能不断添加,但其中大部分往往是语法糖。 另一方面,Java 注重简单性和易于理解。 Java 最初创建的很多原因都是针对 C++ 语法复杂性的反应。

Java has always struck me as a minimalist language - I would imagine that since verbatim strings are not a necessity (like properties for instance) they were not included.

For instance in C# there are many quick ways to do thing like properties:

public int Foo { get; set; }

and verbatim strings:

String bar = @"some
string";

Java tends to avoid as much syntax-sugar as possible. If you want getters and setters for a field you must do this:

private int foo;

public int getFoo() { return this.foo; }
public int setFoo(int foo) { this.foo = foo; }

and strings must be escaped:

String bar = "some\nstring";

I think it is because in a lot of ways C# and Java have different design goals. C# is rapidly developed with many features being constantly added but most of which tend to be syntax sugar. Java on the other hand is about simplicity and ease of understanding. A lot of the reasons that Java was created in the first place were reactions against C++'s complexity of syntax.

嘦怹 2024-07-21 07:10:43

我觉得“为什么”问题很有趣。 C# 是一种较新的语言,并试图改进 Java 等其他语言中被认为存在的缺点。 “为什么”问题的简单原因是 - Java 标准没有像 C# 中那样定义 @ 运算符。

I find it funny "why" questions. C# is a newer language, and tries to improve in what is seen as shortcomings in other languages such as Java. The simple reason for the "why" question is - the Java standard does not define the @ operator such as in C#.

放低过去 2024-07-21 07:10:43

就像所说的,大多数时候你想转义字符是为了正则表达式。 在这种情况下使用:
模式.quote()

Like said, mostly when you want to escape characters is for regexes. In that case use:
Pattern.quote()

叫思念不要吵 2024-07-21 07:10:43

我认为原因之一是正则表达式(这是此类字符串文字的主要原因)直到 Java 1.4 才成为 Java 平台的一部分(如果我没记错的话)。 当语言被定义时,根本就没有那么多的需要。

I think one of the reasons is that regular expressions (which are a major reason for these kind of String literals) where not part of the Java platform until Java 1.4 (if I remember correctly). There simply wasn't so much of a need for this, when the language was defined.

泡沫很甜 2024-07-21 07:10:43

Java(不幸的是)没有这样的东西,但是 Groovy 有

assert '''hello,
world''' == 'hello,\nworld'
//triple-quotes for multi-line strings, adds '\n' regardless of host system
assert 'hello, \
world' == 'hello, world' //backslash joins lines within string

我真的很喜欢这个当我做一些 .NET 工作时,C# 的功能。 它对于剪切和粘贴 SQL 查询特别有用。

Java (unfortunately) doesn't have anything like this, but Groovy does:

assert '''hello,
world''' == 'hello,\nworld'
//triple-quotes for multi-line strings, adds '\n' regardless of host system
assert 'hello, \
world' == 'hello, world' //backslash joins lines within string

I really liked this feature of C# back when I did some .NET work. It was especially helpful for cut and pasted SQL queries.

神爱温柔 2024-07-21 07:10:43

我不确定为什么,但你可以通过转义转义字符来做到这一点。 由于所有转义字符前面都有一个反斜杠,因此通过插入双反斜杠可以有效地取消转义字符。 例如“\now”将产生换行符,然后字母“ow”但“\now”将产生“\now”

I am not sure on the why, but you can do it by escaping the escape character. Since all escape characters are preceded by a backslash, by inserting a double backslash you can effectively cancel the escape character. e.g. "\now" will produce a newline then the letters "ow" but "\now" will produce "\now"

就此别过 2024-07-21 07:10:43

我认为这个问题就像:“为什么java不像Python那样对缩进敏感?”
提到的语法是一个糖,但它是多余的(多余的)。

I think this question is like: "Why java is not indentation-sensitive like Python?"
Mentioned syntax is a sugar, but it is redundant (superfluous).

青丝拂面 2024-07-21 07:10:43

您应该会发现您的 IDE 可以为您处理问题。
如果您位于字符串中间并将原始文本复制粘贴到其中,它应该会为您转义文本。

PERL 有更多种方法来设置字符串文字,有时希望 Java 也支持这些。 ;)

You should find your IDE handles the problem for you.
If you are in the middle of a String and copy-paste raw text into it, it should escape the text for you.

PERL has a wider variety of ways to set String literals and sometimes wish Java supported these as well. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文