2
votes

I would like to work with very long Strings in Eclipse. So my Problem is: Everytime I put in a String in the Brackets Eclipse just recognizes the First line as a String(There are linebreakes in the String)

The String:

public static final String EXAMPLE_TEST =""

When I insert the String:

public static final String EXAMPLE_TEST ="1.    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore 
2.  magna aliquyam erat, sed diam voluptua. 
3.  At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum 
4.  dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore
5.  magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, 
6.  no sea takimata sanctus est Lorem ipsum dolor sit amet.
"

The Lines after 1.("orem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore") get escaped.

I hope someone can help. Thanks

6
And IO would like to avoid adding a "+" sign every time.(As said... very long Strings) - Ahmad
Can you please post your exact code in the code brackets. - JDD
What's your objection to "+"? - Steve H.
The answers below are all correct - you have to use a +. If the reason you want to avoid + is concatenation performance, I would not be concerned, since these are all static final anyway. - sparc_spread
I've never actually tried it (and inspected the resulting .class) to be sure, but when you use + to concatenate literal strings in Java, javac is supposed to recognize that and do the concatenation at compile time. So there is no added runtime overhead, either in time or storage. - Hot Licks

6 Answers

10
votes

In Preferences:

Java > Editor > Typing

Check "Escape text when pasting into a string literal".

4
votes

Typically these are broken up;

String blah = "I am \n" +
 "a very \n" +
 "long string.";

The compiler expects tokens to be closed before new lines. A 'token' is something like 'String' or 'blah' or '=' or '"I am "' or '+'. You can't have a token that is split across multiple lines - the compiler doesn't know how to recognize that. It's fundamental to the language, unfortunately.

4
votes

You can't handle line breaks directly in the String. So you're stuck with concatenating them to each other with + along with a \n character.

This can easily be automated though with a simple find and replace tool.

It might be interesting to note though that there was a proposal for multi-line strings to be added to Java 7. Unfortunately nothing came it (more details can be found in the link).

2
votes

Java doesn't support multiline strings. You have to use + if you don't want everything in one line.

Alternatively, you could load the text from a ini (Preferences) file that you distribute with your source (can be even inside the jar). This is also good when you think about adding i18n later on.

1
votes

There really isn't anyway to do that without the + sign everywhere. The JLS says that:

It is a compile-time error for a line terminator to appear after the opening " and before the closing matching ".

So you're only way to declare such strings in your source code is by having them be on one line and you insert the \r\n in the middle, or you use the + operator to concatenate them. You shouldn't be afraid of using the + operator on the strings since they are declared static final, and thus the concatenation will only be done once.

An alternative would be to read those strings from a file, so that you didn't have to declare them in your source code.

1
votes

Following Java's coding conventions you must concatenate the String values:

public static final String EXAMPLE_TEST = "1.  Lorem ipsum dolor sit amet, "  
                                          + "consetetur sadipscing elitr, sed diam " 
                                          + "nonumy eirmod tempor invidunt ut labore "                                                
                                          + "et dolore "
                                          + "2.  magna aliquyam erat, sed "                                              
                                          + "diam voluptua. "
                                          + "3.  At vero eos et " 
                                          + "accusam et justo duo dolores et ea " 
                                          + "rebum. Stet clita kasd gubergren, no " 
                                          + "sea takimata sanctus est Lorem ipsum "
                                          + "4.  dolor sit amet. Lorem ipsum dolor " 
                                          + "sit amet, consetetur sadipscing elitr, "
                                          + "sed diam nonumy eirmod tempor invidunt "                                    
                                          + "ut labore et dolore " 
                                          + "5.  magna aliquyam "                                        
                                          + "erat, sed diam voluptua. At vero eos et "                                          
                                          + "accusam et justo duo dolores et ea " 
                                          + "rebum. Stet clita kasd gubergren, "
                                          + "6.  no sea takimata sanctus est Lorem " 
                                          + "ipsum dolor sit amet.";

Make sure that the concatenation operator always begins the next line. https://www.oracle.com/technetwork/java/javase/documentation/codeconventions-136091.html#248

Hope this helps!

Happy coding,

Brady