如何调试返回带有值但没有内容的字符串的 Java 字符串拆分?

发布于 2025-01-03 11:17:12 字数 542 浏览 1 评论 0原文

我尝试使用 regEx 来分割一个字符串,这给了我一个包含 2 个项目的数组。 我只对第一个元素真正感兴趣,但是调试器报告它没有任何内容(屏幕截图中的黄色圆圈),但具有我在正则表达式中匹配的项目的“值”。 (屏幕截图中以蓝色突出显示)

在此处输入图像描述

如何以编程方式获取该值?

这是我正在使用的代码:

//Handle Related description by stripping off data relating to it
        String[] descriptionShortened=jsonObject.getString("description").split("^.*[^Related]");
        String descriptionintro=descriptionShortened[0].toString();


        descriptionList.add(descriptionintro);

I trying out using regEx to split a String which gives me an array with 2 items in it.
I am only really interested in the first element, however the debugger is reporting that it has nothing in(in yellow circle on screenshot) it but has a "value" of the item I matched in my Regex. (highlighted in blue on screenshot)

enter image description here

How do I programmatically get to the value?

Here is the code I am using:

//Handle Related description by stripping off data relating to it
        String[] descriptionShortened=jsonObject.getString("description").split("^.*[^Related]");
        String descriptionintro=descriptionShortened[0].toString();


        descriptionList.add(descriptionintro);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

执笏见 2025-01-10 11:17:13

[^Related] 匹配字符类,它匹配除字母 R、e、l、a、t、e、d 之外的所有内容。您的正则表达式将匹配任何内容,这就是数组的第一个元素为空的原因。如果您想要一个包含字符串“Related”之前所有内容的字符串,只需

descriptionShortened = jsonObject.getString("description").split("Related");

在返回数组的第一个元素中的字符串“Related”之前调用 Everything 即可。例如,如果 jsonObject.getString("description") 返回 "abcdRelatedefgh",则上面的代码将返回一个数组 { "abcd", "efgh" }

关于你的第一个问题和屏幕截图,该字符串确实是空的。该值是一个指针,但它指向空白空间。每个字符串都有一个值字段,但这并不意味着它不为空。

[^Related] Matches a Character Class which matches everything except the letters R, e, l, a, t, e, d. Your regex will match anything and that's why the first element of the array is empty. If you wanted to a string that has everything before the string "Related", just call

descriptionShortened = jsonObject.getString("description").split("Related");

Everything before the string "Related" will be in the first element in the array that is returned. For instance, if jsonObject.getString("description") returns "abcdRelatedefgh", the code above would return an array { "abcd", "efgh" }.

In regards to your first question and the screenshot, that String really is empty. The value is a pointer, but it's pointing to empty space. Every String has a value field, but that doesn't mean that it's not empty.

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