有一些生成的代码可以构建地图。放置在地图中的值是多行字符串 示例:
theMap.put("SOMEKEY", "Line 1 of string.
Also line 2 of string.
Perhaps more");
当我尝试将代码复制/粘贴到eclipse时,由于地图中值的格式,我得到红色错误。
我用谷歌搜索了found,这里有一个配置在Eclipse中保存字符串文字中的格式,但在这种情况下似乎不起作用。
还有其他配置选项吗?
答案 0 :(得分:2)
如果您只需要保留空间,请使用\ n字符,如下所示:
theMap.put("SOMEKEY", "Line 1 of string.\nAlso line 2 of string.\nPerhaps more");
或者如果你想要多线:
theMap.put("SOMEKEY", "Line 1 of string."
+"\nAlso line 2 of string."
+"\nPerhaps more");
如果只将字符串值粘贴到空字符串(""
)中,为了能够执行此操作,您需要启用以下设置:window> preferences> java> editor>输入并检查最后一个选项(粘贴到字符串文字时转义文本)
答案 1 :(得分:0)
theMap.put("SOMEKEY", "Line 1 of string.\nAlso line 2 of string.\nPerhaps more");
分成多行:
theMap.put("SOMEKEY", "Line 1 of string.\nAlso line 2 of string.\n" +
"Perhaps more");
如果您想从文件中读取多行:
String s, fullString;
while ((s = bufferedReader.readLine()) != null) {
fullString += s + "\n";
}
bufferedReader.close();
theMap.put("SOMEKEY", fullString);