[英]How to split two lines in java ? while i try to split by /n , the string is printing with garbage value :(
While I tried splite a line by below code. 虽然我尝试通过下面的代码拼接一行。 getting the result some garbage. 得到的结果有些垃圾。 while i printed the value of selectedproduct.get(j).getText()
I'm getting the below string 而我打印selectedproduct.get(j).getText()
我得到以下字符串
Civil War A Nation Divided
Playstation2 Software
I just required the upper one. 我只需要上面一个。
System.out.println(selectedproduct.get(j).getText().split("\\n"));
String.split()
returns an array of values between each occurrence of the delimiter. String.split()
在每次出现的分隔符之间返回一个值数组。 The reason you're getting "garbage" values is because arrays use the default implementation of toString()
. 您获得“垃圾”值的原因是因为数组使用toString()
的默认实现 。 To print the full array, you can use Arrays.toString()
: 要打印完整数组,可以使用Arrays.toString()
:
System.out.println(Arrays.toString(selectedproduct.get(j).getText().split("\\n")));
If you only want the first line, just print the first element of the returned array: 如果您只想要第一行,只需打印返回数组的第一个元素:
System.out.println(selectedproduct.get(j).getText().split("\\n")[0]);
The .split() would create an array of strings so to get teh first part of splitted array use the index as you print. .split()将创建一个字符串数组,以便在打印时使用索引获取分割数组的第一部分。
System.out.println(selectedproduct.get(j).getText().split("\\n")[0]); //index of upper line for you
Also in some cases you might want to use \\r\\n
for platform specific carriage return - 此外,在某些情况下,您可能希望使用\\r\\n
进行特定于平台的回车 -
System.out.println(selectedproduct.get(j).getText().split("\\r\\n")[0]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.