4 回答
TA贡献1846条经验 获得超7个赞
为了删除空格,我们可以使用 trim() 并删除我们需要替换的换行符,
list.stream().map(s -> s.replaceAll("\n",",").trim()).collect(Collectors.toList());
TA贡献1817条经验 获得超6个赞
本质上你有一个包含 2 个元素的列表:
[Menu, General \n Export \n Layer Manager, Reporting \n Theme \n Preferences \n Labels \n Reset Defaults \n Report]
大概是通过这行代码:
String text = list.get(i).getText();
您正在尝试获取第二个元素并将单词打印为List。所以首先你需要将字符串转换General \n Export \n Layer Manager, Reporting \n Theme \n Preferences \n Labels \n Reset Defaults \n Report成一个数组,删除\n然后split()对它们进行编辑。您可以使用以下解决方案:
代码:
String text = list.get(i).getText();
//String text = "General \n Export \n Layer Manager, Reporting \n Theme \n Preferences \n Labels \n Reset Defaults \n Report";
String newText = text.replace(" \n "," ");
String strArray[] = newText.split(" ");
System.out.println("Output as Array List");
for(int i=0; i < strArray.length; i++){
System.out.println(strArray[i]);
控制台输出:
General
Export
Layer
Manager,
Reporting
Theme
Preferences
Labels
Reset
Defaults
Report
现在,您可以使用以下解决方案轻松地将Array转换为List以获得所需的输出:
代码:
List<String>myList = new ArrayList<>();
myList = Arrays.asList(strArray);
System.out.println("Output as List");
System.out.println(myList);
控制台输出:
Output as List
[General, Export, Layer, Manager,, Reporting, Theme, Preferences, Labels, Reset, Defaults, Report]
TA贡献1860条经验 获得超8个赞
如果你用
String enter = System.getProperty("line.separator");
那么我相信你可以在进入时分裂。我猜这让事情变得更容易了。
添加回答
举报