JavaRush/Java Blog/Random EN/Escaping characters

Escaping characters

Published in the Random EN group
members
Hello! In previous lectures, we have already become familiar with strings, which are represented by the class in Java String. As you probably remember, a string is a sequence of characters. The symbols can be anything - letters, numbers, punctuation marks, and so on. The main thing is that when creating a string, the entire sequence is enclosed in quotes:

public class Main {
   public static void main(String[] args) {
       String sasha = new String ("Меня зовут Саша, мне 20 лет!");
   }
}
But what happens if we need to create a string that also has quotes inside it? For example, we want to tell the world about our favorite book:

public class Main {
   public static void main(String[] args) {
       String myFavoriteBook = new String ("Моя любимая книга - "Сумерки" Стефани Майер");
   }
}
The compiler seems to be unhappy with something! What do you think could be the reason for the error, and why did it occur specifically with quotes? The fact is that the compiler perceives quotes in a strictly defined way, namely, it wraps a string in them. And every time he sees a character ", he expects that the same character will follow for it, and between them there will be the text of the line that he, the compiler, must create. In our case, the quotation marks around the word "Twilight" are inside other quotation marks . And when the compiler comes to this piece of text, it simply does not understand what they want from it. It seems like there is a quote, which means it should create a string. But he’s already doing it! This is precisely the reason. Simply put, at this point the compiler misunderstands what they want from it. "Another quote? Is this some kind of error? I'm already creating a string! Or should I create another one? Errr...:/" We need to explain to the compiler when a quote is a command for it ("create a string!"), and when it is a simple symbol (“display the word “Twilight” along with quotation marks!”). To achieve this, Java uses character escaping . This is done using a special character. Like this: \. In ordinary life it is called a "backslash", but in Java it (in combination with the character to be escaped) is called an escape sequence . For example, \"here it is - a control sequence for displaying quotes on the screen. Having encountered such a construction inside your code, the compiler will understand that this is just a “quote” character that needs to be displayed on the screen. Let's try to change our code with the book:

 public static void main(String[] args) {
       String myFavoriteBook = new String ("Моя любимая книга - \"Сумерки\" Стефани Майер");
       System.out.println(myFavoriteBook);
   }
}
We escaped the two "inner" quotes with a \. Let's try to run the method main()... Console output:

Моя любимая книга - "Сумерки" Стефани Майер
Great, the code worked exactly as needed! Quotes are far from the only case where we may need character escaping. For example, we wanted to tell someone about our work:

public class Main {
   public static void main(String[] args) {
       String workFiles= new String ("Мои рабочие файлы лежат в папке D:\Work Projects\java");
       System.out.println(workFiles);
   }
}
And again a mistake! Can you already guess what the reason is? The compiler again does not understand what to do. After all, a symbol \for him is nothing more than a control sequence ! It expects that after the slash there should be some character that it will have to interpret in some special way (for example, a quotation mark). However, here \the regular letters follow. So the compiler is confused again. What to do? Exactly the same as last time: just add \one more to ours \!

public class Main {

   public static void main(String[] args) {

       String workFiles= new String ("Мои рабочие файлы лежат в папке D:\\Work Projects\\java");
       System.out.println(workFiles);

   }
}
Let's see what comes out of this: Console output:

Мои рабочие файлы лежат в папке D:\Work Projects\java
Super! The compiler instantly determined that these \were ordinary symbols that needed to be output to the console along with the rest. There are quite a few escape sequences in Java. Here is their complete list:
  • \t tab character.
  • \b a return character in the text one step back or deleting one character in a line (backspace).
  • \n newline character.
  • \r carriage return character.
  • \f page run.
  • \' single quote character.
  • \" double quote character.
  • \\backslash character (\).
Thus, if the compiler encounters a symbol in the text \n, it will understand that this is not just a symbol and a letter that needs to be output to the console, but a special command for it - “make a line break!” For example, this may be useful to us if we want to output a piece of a poem to the console:

public class Main {
   public static void main(String[] args) {
       String borodino = new String ("Скажи-ка, дядя, \nВедь не даром \nМосква, спаленная пожаром, \nФранцузу отдана?");
       System.out.println(borodino);
   }
}
And this is what we got: Console output:

Скажи-ка, дядя, 
Ведь не даром 
Москва, спаленная пожаром, 
Французу отдана?
Exactly what is needed! The compiler recognized the escape sequence and output a piece of verse in 4 lines.

Unicode

Another important topic you need to know about in connection with character escaping is Unicode . Unicode is a character encoding standard that includes characters from almost all written languages ​​of the world. In other words, this is a list of special codes, in which there is a code for almost any character from any language! Naturally, this list is very large and no one learns it by heart :) If you are interested in where it came from and why it was needed, read the informative article on Habrahabr . All character codes in Unicode are “letter u+ hexadecimal digit”. For example, the famous copyright symbol is indicated by the code 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 let everyone know that this lecture is copyrighted by JavaRush:

public class Main {
   public static void main(String[] args) {
       System.out.println("Лекция \"Экранирование символов\", \u00A9 2018 Javarush");
   }
}
Console output:

Лекция "Экранирование символов", © 2018 Javarush
Great, everything worked out! But special characters aren't everything! Using Unicode and character escaping, you can encode text written in different languages ​​at the same time. And even in several different dialects of the same language!

public class Main {
   public static void main(String[] args) {

       System.out.println("\u041c\u0430\u0301\u043e " +
               "\u0426\u0437\u044d\u0434\u0443\u0301\u043d " +
               "\u0028\u043a\u0438\u0442\u002e \u0442\u0440\u0430\u0434\u002e " +
               "\u6bdb\u6fa4\u6771\u002c \u0443\u043f\u0440\u002e " +
               "\u6bdb\u6cfd\u4e1c\u002c \u043f\u0438\u043d\u044c\u0438\u043d\u044c\u003a " +
               "\u004d\u00e1\u006f \u005a\u00e9\u0064\u014d\u006e\u0067\u0029 " +
               "\u2014 \u043a\u0438\u0442\u0430\u0439\u0441\u043a\u0438\u0439 " +
               "\u0433\u043e\u0441\u0443\u0434\u0430\u0440\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 " +
               "\u0438 \u043f\u043e\u043b\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 " +
               "\u0434\u0435\u044f\u0442\u0435\u043b\u044c \u0058\u0058 \u0432\u0435\u043a\u0430\u002c " +
               "\u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u043a " +
               "\u043c\u0430\u043e\u0438\u0437\u043c\u0430\u002e");
   }
}
Console output:

Ма́о Цзэду́н (кит. трад. 毛澤東, упр. 毛泽东, пиньинь: Máo Zédōng) — китайский государственный и политический деятель XX века, главный теоретик маоизма.
In this example, knowing the character codes, we wrote a string consisting of the Cyrillic alphabet and three (!) different types of writing Chinese characters - classical, simplified and Latin (pinyin). That's basically it! Now you know enough about character escaping to use this tool in your work :) If everything is not completely clear, I advise you to read this article : it will be a good addition.
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet