Java:split() 返回 [Ljava.lang.String;@186d4c1],为什么? [英] Java: split() returns [Ljava.lang.String;@186d4c1], why?

查看:18
本文介绍了Java:split() 返回 [Ljava.lang.String;@186d4c1],为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么!

我基本上有一个字符串(是的,不是数组),它具有以下内容:

I bascially have a STRING (yes, not an array), that has the following contents:

[something, something else, somoething, trallala, something]

我想把它变成一个字符串[].所以首先我 substring() 关闭第一个和最后一个字符来去掉括号 [].然后我使用 split() 函数以逗号分隔.我尝试同时使用|"和 "," 和 "," 具有相同的结果.

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.

这是我得到的:

[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(",");

正如你所看到的,第一部分是 (matcher.group(3).toString()),它确实返回一个有效的字符串(就像我上面发布的例子).所以我不明白为什么它不起作用.

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.

有什么想法吗?

我稍微澄清了代码:

String arrString = matcher.group(3).toString();
int length = arrString.length();
String[] urlArr = (arrString.substring(1, length-1)).split(",");
System.out.println(urlArr);

推荐答案

您正在获得一个有效的 String 数组,但是尝试直接打印它并没有达到您期望的效果.尝试例如

You are getting a valid array of Strings, but trying to print it directly does not do what you would expect it to do. Try e.g.

System.out.println(Arrays.asList(urlArr));

更新

如果你想一个一个地处理解析的token,你可以简单地遍历数组,例如

Update

If you want to process the parsed tokens one by one, you can simply iterate through the array, e.g.

for (String url : urlArr) {
  // Do something with the URL, e.g. print it separately
  System.out.println("Found URL " + url);
}

这篇关于Java:split() 返回 [Ljava.lang.String;@186d4c1],为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆