java 字符串中转义字符_Java中的转义字符

java 字符串中转义字符

Learn how we can use escape sequence in Java

了解如何在Java中使用转义序列

These characters can be any letters, numerals, punctuation marks and so on. The main thing when creating a string is that the entire sequence must be enclosed in quotation marks:

这些字符可以是任何字母,数字,标点符号等。 创建字符串时,主要的事情是整个序列必须用引号引起来:

public class Main {
       
public static void main(String[] args) {
String alex = new String ("My name is Alex. I'm 20!");
}
}

But what do we do if we need to create a string that itself must contain quotation marks? For example, suppose we want to tell the world about your favorite book:

但是,如果我们需要创建一个本身必须包含引号的字符串,该怎么办? 例如,假设我们想向全世界介绍您最喜欢的书:

public class Main {
       
public static void main(String[] args) {
String myFavoriteBook = new String ("My favorite book is "Twilight" by Stephanie Meyer");
}
}

It seems the compiler is unhappy about something! What do you think the problem could be? And what does it have to do with quotation marks? In fact, it’s all very simple. The compiler interprets quotation marks in a very specific way, i.e. it expects strings to be wrapped in them. And every time the compiler sees “, it expects that the quotation mark will be followed by a second quotation mark, and that the content between them is the text of a string to be created by the compiler. In our case, the quotation marks around the word “Twilight” are inside other quotation marks. When the compiler reaches this piece of text, it simply doesn’t understand what it is expected to do. The quotation mark suggests that a string must be created. But that’s what the compiler is already doing! Here’s why: simply speaking, the compiler gets confused about what it is expected to do. “Another quotation mark? Is this some kind of mistake? I’m already creating a string! Or should I create another one? Argh!…:/” We need to let the compiler know when a quotation mark is a command (“create a string!”) and when it is simply a character (“display the word “Twilight” along with quotation marks!”). To do this, Java uses character escaping. This is accomplished using a special symbol: \. This symbol is normally called “backslash”. In Java, a backslash combined with a character to be “escaped” is called a control sequence. For example, \" is a control sequence for displaying quotation marks on the screen. Upon encountering this construct in your code, the compiler will understand that this is just a quotation mark that should be displayed on the screen. Let’s try changing our code with the book:

似乎编译器对某些事情不满意! 您认为问题可能是什么? 与引号有什么关系? 实际上,这非常简单。 编译器以非常特定的方式解释引号,即,它期望将字符串包装在其中。 并且每次编译器看到“”时,它都希望引号后跟第二个引号,并且它们之间的内容是编译器要创建的字符串的文本。 在我们的情况下,单词“ Twilight”周围的引号位于其他引号内。 当编译器到达这段文本时,它根本不理解它的预期功能。 引号表明必须创建一个字符串。 但这就是编译器已经在做的! 原因如下:简单来说,编译器对预期的功能感到困惑。 “另一个引号? 这是某种错误吗? 我已经在创建字符串了! 还是我应该再创建一个? Argh!…:/“我们需要让编译器知道何时引号是命令(“创建字符串!”)以及何时仅是字符(“将单词“ Twilight”与引号一起显示!”)。 。 为此,Java使用字符转义。 这可以通过使用特殊符号\来完成。 此符号通常称为“反斜杠”。 在Java中,反斜杠与要“转义”的字符结合在一起称为控制序列。 例如, \"是用于在屏幕上显示引号的控制序列。在您的代码中遇到此构造时,编译器将理解这只是应该在屏幕上显示的引号。让我们尝试使用这本书:

public static void main(String[] args) {
       
String myFavoriteBook = new String ("My favorite book is \"Twilight\" by Stephanie Meyer");
System.out.println(myFavoriteBook);
}
}

We’ve used \ to escape our two “internal” quotation marks. Let’s try running the main() method… Console output: My favorite book is “Twilight” by Stephanie Meyer Excellent! The code worked exactly how we wanted it to! Quotation marks are by no means the only characters we may need to escape. Suppose we want to tell someone about our work:

我们使用\来转义两个“内部”引号。 让我们尝试运行main()方法…控制台输出:我最喜欢的书是斯蒂芬妮·迈耶(Stephanie Meyer)的《暮光之城》(Twilight),太好了! 该代码完全按照我们希望的那样工作! 引号绝不是我们可能需要转义的唯一字符。 假设我们想告诉某人我们的工作:

public class Main {
       
public static void main(String[] args) {
String workFiles= new String ("My work files are in D:\Work Projects\java");
System.out.println(workFiles);
}
}

Another error! Can you guess why? Once again, the compiler doesn’t understand what to do. After all, the compiler doesn’t know \ as anything other than a control sequence! It expects the backslash to be followed by a certain character that it must somehow interpret in a special way (such as a quotation mark). But, in this case, \ is followed by ordinary letters. So the compiler is confused again. What should we do? Exactly the same thing as before: we just add another \ to our \!

另一个错误! 你能猜出为什么吗? 再次,编译器不知道该怎么做。 毕竟,除了控制序列以外,编译器不知道\ ! 它期望在反斜杠后面跟随一个必须以某种特殊方式(例如,引号)进行解释的特定字符。 但是,在这种情况下, \后跟普通字母。 因此,编译器再次感到困惑。 我们应该做什么? 同样的事情像以前一样:我们仅添加了另一个\我们\

public class Main {   public static void main(String[] args) {       String workFiles= new String ("My work files are in D:\\Work Projects\\java");
System.out.println(workFiles); }
}

Let’s see what we get: Console output: My work files are in D:\Work Projects\java Super! The compiler immediately determines that the \ are ordinary characters that should be displayed along with the rest. Java has quite a lot of control sequences. Here’s the full list:

让我们看看得到了什么:控制台输出:我的工作文件在D:\ Work Projects \ java Super中! 编译器立即确定\是应与其余字符一起显示的普通字符。 Java有很多控制序列。 这是完整列表:

  • \t – tab.

    \t标签。

  • \b – backspace (a step backward in the text or deletion of a single character).

    \b退格键(在文本中向后退一步或删除单个字符)。

  • \n – new line.

    \n新行。

  • \r – carriage return. ()

    \r回车。 ()

  • \f – form feed.

    \f换页。

  • \' single quote.

    \'单引号。

  • \" double quote.

    \"双引号。

  • \\ backslash.

    \\反斜杠。

Thus, if the compiler encounters \n in the text, it understands that this is not just a symbol and a letter to display on the console, but rather a special command to “move to a new line!”. For example, this may be useful if we want to display part of a poem:

因此,如果编译器在文本中遇到\n ,它将理解这不仅是在控制台上显示的符号和字母,而且是“移至新行!”的特殊命令。 例如,如果我们想显示一首诗,这可能很有用:

public class Main {
       
public static void main(String[] args) {
String byron = new String ("She walks in beauty, like the night, \nOf cloudless climes and starry skies\nAnd all that's best of dark and bright\nMeet in her aspect and her eyes...");
System.out.println(byron);
}
}

Here’s what we get: Console output: She walks in beauty, like the night, Of cloudless climes and starry skies And all that’s best of dark and bright Meet in her aspect and her eyes… Just what we wanted! The compiler recognized the escape sequence and output an excerpt of the poem on 4 lines.

这就是我们得到的结果:控制台输出:她像夜晚一样走在美丽的地方,无云的气候和繁星密布的天空,以及最好的黑暗与明亮,在她的表情和眼睛中相遇……正是我们想要的! 编译器识别出转义序列,并在4行上输出该诗的摘录。

转义Unicode字符 (Escape Unicode characters)

Another important topic that you need to know about in connection with escape characters is Unicode. Unicode is a standard character encoding that includes the symbols of almost every written language in the world. In other words, it’s a list of special codes that represent nearly every character in any language! Naturally, this is a very long list and nobody learns it by heart :) If you want to know where it came from and why it became necessary, read this informative article: https://docs.oracle.com/javase/tutorial/i18n/text/unicode.html All Unicode character codes have the form “u+<hexadecimal digit>”. For example, the well-known copyright symbol is represented by u00A9. So, if you need to use this character when working with text in Java, you can escape it in your text! For example, we want to inform everyone that Kajal Rawal owns the copyright to this lesson:

关于转义字符,您需要了解的另一个重要主题是UnicodeUnicode是一种标准的字符编码,其中包含世界上几乎所有书面语言的符号。 换句话说,这是一列特殊代码的清单,几乎代表任何语言中的每个字符! 自然地,这是一个非常长的列表,没有人会真正地学习它:)如果您想知道它的来源以及为什么有必要,请阅读这篇内容丰富的文章: https : //docs.oracle.com/javase/tutorial/ i18n / text / unicode.html所有Unicode字符代码均采用“ u + <十六进制数字>”的形式。例如,众所周知的版权符号由u00A9表示。因此,如果在处理文本时需要使用此字符在Java中,您可以在文本中对其进行转义!例如,我们想通知所有人,Kajal Rawal拥有本课程的版权:

public class Main {
       
public static void main(String[] args) {
System.out.println("\"Escaping characters\", \u00A9 2020 KajalRawal");
}
}

Console output: “Escaping characters”, © 2020 KajalRawal Great, it all worked out! But it’s not just about special symbols! You can use Unicode and escape characters to encode text written simultaneously in different languages. And even text written in several different dialects of the same language!

控制台输出: “转义字符”,©2020 KajalRawal太好了,一切顺利! 但这不只是特殊符号! 您可以使用Unicode和转义字符来编码同时用不同语言编写的文本。 甚至是用相同语言的几种不同方言编写的文本!

And that about sums it up! Now you know enough about escaping characters to use this great tool in your work :)

翻译自: https://medium.com/swlh/escape-characters-in-java-7190f4be9bc0

java 字符串中转义字符

    原文作者:weixin_26720549
    原文地址: https://blog.csdn.net/weixin_26720549/article/details/109070353
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞