Java: split() 返回 [Ljava.lang.String;@186d4c1],为什么?
我不知道为什么!
我基本上有一个 STRING (是的,不是数组),它包含以下内容:
[something, something else, somoething, trallala, something]
我想把它变成一个 String[]。因此,首先我将第一个和最后一个字符的 substring() 去掉,以去掉括号 []。然后我使用 split() 函数以逗号分隔。我尝试同时使用“\|”和“,”和“\,”具有相同的结果。
这就是我得到的:
[Ljava.lang.String;@186d4c1
这是它的代码。我把它做成了一行:
String[] urlArr = ((matcher.group(3).toString()).substring(1, (matcher.group(3).length()-1))).split(",");
正如你所看到的,第一部分是 (matcher.group(3).toString()),它确实返回一个有效的字符串(就像我上面发布的示例)。所以我不明白为什么它不起作用。
有什么想法吗?
编辑:
我稍微澄清了代码:
String arrString = matcher.group(3).toString();
int length = arrString.length();
String[] urlArr = (arrString.substring(1, length-1)).split(",");
System.out.println(urlArr);
And i have no idea why!
I bascially have a STRING (yes, not an array), that has the following contents:
[something, something else, somoething, trallala, something]
And i want to turn it into a String[]. So first off i substring() off the first and the last character to get rid of the brackets []. Then i use the split() function to split by comma. I tried using both "\|" and "," and "\," with the same results.
This is what i get:
[Ljava.lang.String;@186d4c1
Here's the code for it. I made it into a one-liner:
String[] urlArr = ((matcher.group(3).toString()).substring(1, (matcher.group(3).length()-1))).split(",");
As you can see the first part is (matcher.group(3).toString()), and it DOES return a valid string (like the example i posted above). So i don't get why it's not working.
Any ideas?
EDIT:
I clarified the code a bit:
String arrString = matcher.group(3).toString();
int length = arrString.length();
String[] urlArr = (arrString.substring(1, length-1)).split(",");
System.out.println(urlArr);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
输出
是java默认打印字符串数组(或任何数组)的方式(当转换为字符串时)。您可以使用
以获得更具可读性的版本。
The output
is the way java prints a string array (or any array) by default (when converted to a string). You may use
to get a more readable version.
您正在获取一个有效的
String
数组,但尝试直接打印它并不能实现您期望的效果。尝试例如更新
如果你想一一处理解析的令牌,你可以简单地迭代数组,例如
You are getting a valid array of
String
s, but trying to print it directly does not do what you would expect it to do. Try e.g.Update
If you want to process the parsed tokens one by one, you can simply iterate through the array, e.g.
当您对数组执行
toString
时,您只会获得内部表示形式。没那么有用。尝试 Arrays.toString(what returned from split) 以获得更具可读性的内容。When you do
toString
on an Array, you just get the internal representation. Not that useful. TryArrays.toString(what comes back from split)
for something more readable.