Translating escape sequences programmatically – Text blocks, Locales, Numbers & Math
Translating escape sequences programmatically – Text blocks, Locales, Numbers & Math
24, Feb 2024
Translating escape sequences programmatically – Text blocks, Locales, Numbers & Math
7. Translating escape sequences programmatically
We already know that the compiler is responsible for the translation of escape sequences and most of the time there is no need to explicitly interfere in this process. But there are cases when we may need programmatic access to this process (for instance, to explicitly un-escape a string before passing it to a function). Starting with JDK 15, we can accomplish this via String.translateEscapes() which is capable of un-escape sequences such as \t, \n, \b, and so on, and octal numbers (\0 – \377). However, this method doesn’t translate Unicode escapes (\uXXXX).We can perform an equality test meant to reveal how translateEscapes() work:
As you already intuit, the result is yes.Next, let’s assume that we want to use an external service that prints address on parcels. The function responsible for this task gets a string representing the address without containing escape sequences. The problem is that our customer’s addresses pass through a formatting process that patches them with escape sequences as in the following example:
String address = “”” JASON MILLER (\\”BIGBOY\\”)\\n \\tMOUNT INC\\n \\t104 SEAL AVE\\n \\tMIAMI FL 55334 1200\\n \\tUSA “””;
The following figure reveals how the resulting string will look if we don’t translate escapes of the address (left side) and how it will look if we do (right side). Of course, our goal is to obtain the address from the right side and send it to print:
Figure 1.5 – We want the string from the right side
Translation of escapes can be done programmatically via String.translateEscapes() right before sending the result to the external service. So, here is the code:
Now, translatedAddress can be passed to the external printing service. As an exercise, you can think about how to exploit this method for writing a parser of source code provided via Java or another programming language.
Similar results (of course, read the documentation to obtain the fine-grained information) can be obtained via Apache Commons Lang third-party library support. Please consider StringEscapeUtils.unescapeJava(String).
Next, let’s talk about embedding expressions in text blocks.
24, Feb 2024
Translating escape sequences programmatically – Text blocks, Locales, Numbers & Math
7. Translating escape sequences programmatically
We already know that the compiler is responsible for the translation of escape sequences and most of the time there is no need to explicitly interfere in this process. But there are cases when we may need programmatic access to this process (for instance, to explicitly un-escape a string before passing it to a function). Starting with JDK 15, we can accomplish this via String.translateEscapes() which is capable of un-escape sequences such as \t, \n, \b, and so on, and octal numbers (\0 – \377). However, this method doesn’t translate Unicode escapes (\uXXXX).We can perform an equality test meant to reveal how translateEscapes() work:
String newline = “\\n”.translateEscapes();
System.out.println((“\n”.equals(newline)) ? “yes” : “no”);
As you already intuit, the result is yes.Next, let’s assume that we want to use an external service that prints address on parcels. The function responsible for this task gets a string representing the address without containing escape sequences. The problem is that our customer’s addresses pass through a formatting process that patches them with escape sequences as in the following example:
String address = “””
JASON MILLER (\\”BIGBOY\\”)\\n
\\tMOUNT INC\\n
\\t104 SEAL AVE\\n
\\tMIAMI FL 55334 1200\\n
\\tUSA
“””;
The following figure reveals how the resulting string will look if we don’t translate escapes of the address (left side) and how it will look if we do (right side). Of course, our goal is to obtain the address from the right side and send it to print:
Figure 1.5 – We want the string from the right side
Translation of escapes can be done programmatically via String.translateEscapes() right before sending the result to the external service. So, here is the code:
String translatedAddress = address.translateEscapes();
Now, translatedAddress can be passed to the external printing service. As an exercise, you can think about how to exploit this method for writing a parser of source code provided via Java or another programming language.
Similar results (of course, read the documentation to obtain the fine-grained information) can be obtained via Apache Commons Lang third-party library support. Please consider StringEscapeUtils.unescapeJava(String).
Next, let’s talk about embedding expressions in text blocks.