1

There is some generated code that builds a map. The values placed in map are multiline strings.
Example:

theMap.put("SOMEKEY", "Line 1 of string.  
               Also line 2 of string.  
               Perhaps more"); 

When I try to copy/paste the code to eclipse I get red errors due to the format of the value in the map.
I googled and found and here that there is a configuration in Eclipse to preserve the formatting in String literals but it does not seem to work in this case.
Is there any other configuration option?

Community
  • 1
  • 1
Jim
  • 18,826
  • 34
  • 135
  • 254
  • How is that code generated? – jlordo Jan 09 '13 at 13:40
  • Replace the line breaks by `"\n"`with a text-editor of your choice ore in eclipse. If you do not post process it by hand use an editor with macro features and write a macro than detects only the line breaks you want to replace. – MrSmith42 Jan 09 '13 at 13:45
  • @MrSmith42:How do I declare the line breaks in the replace function of the text-editor in order to replace them with `\n`? – Jim Jan 09 '13 at 13:56
  • In the find filed: simply copy and paste a newline from the text and in the replace filed write \n (as two characters) – MrSmith42 Jan 09 '13 at 14:01

2 Answers2

1

If you only need to keep space, use the \n character like this:

theMap.put("SOMEKEY", "Line 1 of string.\nAlso line 2 of string.\nPerhaps more"); 

Or if you want it multiline :

theMap.put("SOMEKEY", "Line 1 of string."
               +"\nAlso line 2 of string."
               +"\nPerhaps more");

If you paste only the string value into empty string (""), to be able to do this you need to have the following setting enabled: window>preferences>java>editor>typing and check the last option (escape text when pasting into a string literals)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jerome Cance
  • 8,103
  • 12
  • 53
  • 106
  • My problem is that I need to **paste the existing fragments**. Not how to modify them – Jim Jan 09 '13 at 13:41
  • I've edited my post with an option which can help you. You must paste a string literal into empty quote for it to run – Jerome Cance Jan 09 '13 at 13:46
  • What does not work ? if you have the code : theMap.put("SOMEKEY", ""); and you paste your string into the empty string, it should format as expected if the option is checked – Jerome Cance Jan 09 '13 at 13:59
0
theMap.put("SOMEKEY", "Line 1 of string.\nAlso line 2 of string.\nPerhaps more");

To split into multiple lines:

theMap.put("SOMEKEY", "Line 1 of string.\nAlso line 2 of string.\n" +
        "Perhaps more");

If you want to read multiple lines from a file:

String s, fullString;
while ((s = bufferedReader.readLine()) != null) {
    fullString += s + "\n";
}
bufferedReader.close();
theMap.put("SOMEKEY", fullString);
tckmn
  • 57,719
  • 27
  • 114
  • 156
  • You mean do this manually?My problem is that I need to **paste the existing fragments**. Not how to modify them – Jim Jan 09 '13 at 13:41
  • @Jim Why do you need to paste the existing fragments? I could think of another way: you could read from a file. – tckmn Jan 09 '13 at 13:43