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

发布于 2024-12-06 03:47:11 字数 832 浏览 1 评论 0原文

我不知道为什么!

我基本上有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趴在窗边数星星i 2024-12-13 03:47:11

输出

[Ljava.lang.String;@186d4c1

是java默认打印字符串数组(或任何数组)的方式(当转换为字符串时)。您可以使用

Arrays.toString(urlArr)

以获得更具可读性的版本。

The output

[Ljava.lang.String;@186d4c1

is the way java prints a string array (or any array) by default (when converted to a string). You may use

Arrays.toString(urlArr)

to get a more readable version.

调妓 2024-12-13 03:47:11

您正在获取一个有效的 String 数组,但尝试直接打印它并不能实现您期望的效果。尝试例如

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

更新

如果你想一一处理解析的令牌,你可以简单地迭代数组,例如

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

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));

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);
}
羁〃客ぐ 2024-12-13 03:47:11

当您对数组执行 toString 时,您只会获得内部表示形式。没那么有用。尝试 Arrays.toString(what returned from split) 以获得更具可读性的内容。

When you do toString on an Array, you just get the internal representation. Not that useful. Try Arrays.toString(what comes back from split) for something more readable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文