Loading
24, Aug 2024
Creating a multiline SQL, JSON, and HTML string – Text blocks, Locales, Numbers & Math

1. Creating a multiline SQL, JSON, and HTML string Let’s consider the following SQL multiline string: As common knowledge, before JDK 8, we could wrap this SQL as a Java String (string literal) in several ways. Before JDK 8 Probably, the most common approach relies on straightforward concatenation via the…

24, Jul 2024
Exemplifying the usage of text block delimiters – Text blocks, Locales, Numbers & Math

2. Exemplifying the usage of text block delimiters Remember from the previous problem, Creating a multiline SQL, JSON, and HTML string, that a text block is syntactically delimited by an opening and a closing delimiter represented by three double quotation marks, “””.The best approach for exemplifying the usage of these…

24, Jun 2024
Working with indentation in text blocks – Text blocks, Locales, Numbers & Math

3. Working with indentation in text blocks Indentation in text blocks is easy to understand if we have a clear picture of two terms: incidental (or unessential) white spaces – represent the meaningless white spaces that result from code formatting (leading white spaces commonly added by the IDE) or are…

15, May 2024
Removing incidental white spaces in text blocks – Text blocks, Locales, Numbers & Math

4. Removing incidental white spaces in text blocks Removing incidental white spaces in text blocks is typically a job accomplished by the compiler via a special algorithm. To understand the main aspects of this algorithm let’s overlap it on the following example: String json = “””                     |Compiler:—-{                                 |Line 01: 4 …

20, Apr 2024
Using text blocks just for readability – Text blocks, Locales, Numbers & Math

5. Using text blocks just for readability Using text blocks just for readability can be translated as making a string look like a text block but act as a single-line string literal. This is especially useful for formatting long lines of text. For instance, we may want to have the…

24, Mar 2024
Escaping quotes and line terminators in text blocks – Text blocks, Locales, Numbers & Math

6. Escaping quotes and line terminators in text blocks Escaping double quotes is necessary only when we want to embed in the text block the sequence of three double quotes (“””) as follows: String txt = “””             She told me                    \”””I have no idea what’s going on\”””             “””; So escaping…

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…

24, Jan 2024
Formatting text blocks with variables/expressions – Text blocks, Locales, Numbers & Math

8. Formatting text blocks with variables/expressions In Java, it is a common practice to format string literals with variables/expressions to obtain dynamic strings. For instance, we can create a dynamic piece of JSON string via the well-known concatenation as follows: String fn = “Jo”;String ln = “Kym”;String str = “<user><firstName>”…

24, Dec 2023
Adding comments in text blocks – Text blocks, Locales, Numbers & Math

9. Adding comments in text blocks Question: Can we add comments in text blocks?Official answer (Java Language Specification): The lexical grammar implies that comments do not occur within character literals, string literals, or text blocks. Hacks: We may try a hack like the following, but I really don’t recommend it:…

24, Oct 2023
Checking if two text blocks are isomorphic – Text blocks, Locales, Numbers & Math

11. Checking if two text blocks are isomorphic Two text blocks are isomorphic if the resulting strings are isomorphic. Two string literals are considered isomorphic if we can map every character of the first string to every character of the second string in a one-to-one fashion.For example, consider that the…