简体   繁体   English

在eclipse的复制/粘贴中保留格式

[英]Preserve formatting in copy/paste of eclipse

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. 当我尝试将代码复制/粘贴到eclipse时,由于地图中的值格式,我得到红色错误。
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. 我用google搜索并发现 Eclipse中有一个配置来保存String文字中的格式,但在这种情况下似乎不起作用。
Is there any other configuration option? 还有其他配置选项吗?

If you only need to keep space, use the \\n character like this: 如果您只需要保留空间,请使用\\ n字符,如下所示:

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) 如果只将字符串值粘贴到空字符串( "" )中,为了能够执行此操作,您需要启用以下设置:window> preferences> java> editor> typing并检查最后一个选项(粘贴时的转义文本)字符串文字)

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM