Questions tagged [java-text-blocks]

A text block is a multi-line string literal feature in Java 15 and later. Avoids the need for most escape sequences, automatically formats the string in a predictable way, and gives the developer control over the format when desired.

The Text Blocks feature is built into Java 15, after having been previewed in Java 12, 13, & 14.

See the specifications:

16 questions
35
votes
3 answers

How to have placeholder for variable value in Java Text Block?

How can I put a variable into Java Text Block? Like this: """ { "someKey": "someValue", "date": "${LocalDate.now()}", } """
Dennis Gloss
  • 2,307
  • 4
  • 21
  • 27
19
votes
6 answers

Triple quotes in Java like Scala

In Scala you can do something like this: val expr = """ This is a "string" with "quotes" in it! """ Is there something like this in Java? I abhor using "\"" to represent strings with quotes in them. Especially when composing key/value pairs in…
HiChews123
  • 1,598
  • 4
  • 20
  • 39
10
votes
1 answer

Can IntelliJ convert my old string concatenation to new Text Block feature previewed in Java 14?

I have existing code such as this: String sql = "CREATE TABLE " + tableName + " (\n" + " id_ UUID DEFAULT random_uuid() PRIMARY KEY ,\n" + " when_ TIMESTAMP WITHOUT TIME ZONE NOT NULL\n" + " duration_ STRING NOT NULL\n" + …
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
6
votes
1 answer

Java Text Blocks: Mix of Tabs and Spaces within Indentation Prefixes

Java 15 introduced (non-preview) text blocks feature. It allows to define multi-lined string literals without breaking code indentation, by stripping the common white space prefix from the lines. The algorithm is described in JEP 378. But how…
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
5
votes
1 answer

Better way to hard-code JSON data as String in Java

I need to keep JSON data in my code for unit-tests as string. For instance, { "one": 1, "two": 2 } I know that I can read this data from file or from property, but I need to keep them as string. Currently, I have something like this: String…
Maxim Suslov
  • 4,335
  • 1
  • 35
  • 29
5
votes
2 answers

Text blocks in a single line

Is it possibly to use Javas text blocks feature (Java 15) but only write a single line? It seems that I am forced to write multiple lines. For example, I want write this in one line to avoid escaping the " in it String text = """
SL5net
  • 2,282
  • 4
  • 28
  • 44
3
votes
1 answer

How to output a Multiline Character Image in the console

I am developing a text-based game and I want to make the title something catchy. I tried to use a text to ASCII converter to make a nice-looking title and then copy and paste it in my code to output it, but it didn't work. Here is what I tried to…
3
votes
1 answer

Why were the translateEscapes & stripIndent String methods included with the text block feature in Java 15?

In Java 15, JEP 378: Text Blocks added three instance methods to String: stripIndent(), translateEscapes(), and formatted(Object... args): The following methods will be added to support text blocks; String::stripIndent(): used to strip away…
M. Justin
  • 14,487
  • 7
  • 91
  • 130
3
votes
1 answer

Can IntelliJ treat a Java text block as syntax such as SQL or XML to assist editing/formatting?

Java 15 brings us the new JEP 378: Text Blocks feature. This eases working with multi-line strings. Can IntelliJ be made to parse the text within that block as some kind of syntax? Is there a way for me to tell IntelliJ that a text block is SQL…
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
2
votes
1 answer

What is the Text Blocks (or Multiline Strings) feature in Java?

Java SE 13 introduced Text Blocks (or Multiline Strings) feature. What are its differences and the similarities with the existing string representation?
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1
vote
1 answer

How to apply indentation to all lines when injecting multi-line strings in formatted text blocks

I started using Java 15 text blocks which are nice for readability. Now I want to format text blocks and inject strings in them, and from what I understand the idiomatic way to do this right now is to use String::formatted. But it seems that…
Namefie
  • 117
  • 1
  • 2
  • 14
0
votes
1 answer

Kotlin multiline-string annotation parameters

In Java, now that it supports text blocks, you can do this: @Schema(description = """ Line one. Line two. """) public void someMethodName() { ... } In Java, text blocks are compile-time constants and they…
k314159
  • 5,051
  • 10
  • 32
0
votes
1 answer

Java text block indentation and leading spaces

Given the following code public class TextBlock { public static void main(String[] args) { String indentedText = """ hello indented world """; System.out.println(indentedText); …
Wojciech Wirzbicki
  • 3,887
  • 6
  • 36
  • 59
0
votes
1 answer

Text Block and Println Formatting, Java: How to Remove Extra Lines

This is for a hangman game, and the logic works great- the word fills in correctly, and the hangman gets more and more hanged with each word. However, the "graphics" are a little difficult for the user, as you can see below in the output: 3 […
0
votes
3 answers

Java 14 text block leading \r\n inserted when used in Eclipse 4.15.0

I am in the process of learning how to cope with java 14 (preview) text blocks. When using following text block in a Junit test I run across the following unexpected feature (simplified code example, in the real test I use an HTML…
PaulN
  • 63
  • 2
  • 10
1
2