The use of \" in a text block is permitted, but not necessary or recommended. The content may include double quote characters directly, unlike the characters in a string literal. You just need to enclose your string within delimiters, that is a sequence of three double quote characters ( """). This makes it much simpler and is very easy to read and understand. Our multi-line string now becomes- String myMultiLineText = """ first line second line """ The text blocks feature allows you to create multi-line strings, in an intuitive way, the way it is supposed to be.Ī text block is a multi-line string literal that avoids the need for most escape sequences, automatically formats the string in a predictable way, and gives the developer control over format when desired. But, you can see that none of these, looks clean or easy to read, could lead to more error prone code. So, there are many ways to create such multi-line strings, where depending on the use case, one might be better than other. join(ImmutableList.of("first line", "second line")) toString() Using Guava Joiner String myMultiLineTextUsingGuavaJoiner = Joiner.on(newLineChar). concat("second line") String myMultiLineTextUsingJoin = String.join(newLineChar, "first line", "second line") Using StringBuilder StringBuilder myMultiLineStringBuilder = new StringBuilder(). Using String concat/join String myMultiLineTextUsingConcat = "first line". We can use String methods like concat, join or use libraries like StringBuilder, Guava Joiner to make it a bit cleaner. Imagine the multi-line string with more (>5) lines! Not so easy on the eyes, right. Ok, very quickly, my simple code became more verbose and looks cluttered. Here is the updated code- String newLineChar = System.getProperty("parator") String myMultiLineText = "This is first line of my multi-line string."+newLineChar+"I'm on second line now" Java has a way to get the line separator from system properties. Now, to make my code more generic, I have to account for such cases. So, if you are using Windows/Mac OS 9 or before, you might have to use “\r” or “\r\n” as the escape sequence. But, actually, the line separator is dependent on the operating system. Here, by adding “\n”, I’m adding a new line to the string. This will produce the following output: This is first line of my multi-line string. String myMultiLineText = "This is first line of my multi-line string.\nI'm on second line now" To create this, you need to add line separators to the text, so that the compiler understands how to render that string. package multi-line string is a block of text, that spans across multiple lines. The content ends at the last character before the first double quote of the closing delimiter. The closing delimiter is a sequence of three double quote characters. The content begins at the first character after the line terminator of the opening delimiter. In a text block, the opening delimiter is a sequence of three double quote characters (""") followed by zero or more white spaces followed by a line terminator. This is a preview language feature in JDK 13. automatically formats the string in a predictable way, and gives the developer control over format when desired.That means we don't have to escape character like double quote (") avoids the need for most escape sequences.a multi-line string literal that can be written without + concatenation.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |