尝试使用String.split("\\?")时发生意外行为 [英] Unexpected behaviour when trying to use String.split("\\?")

查看:64
本文介绍了尝试使用String.split("\\?")时发生意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个像这样的字符串:

So I have a string that is like this:

"Some text here?Some number here"

我需要拆分它们,我正在使用 String.split("\\?"),但是如果我有这样的字符串:

and I need to split those, I am using String.split("\\?"), but if I have a string like this:

"This is a string with, comma?1234567"

我也用逗号分割了它().如果我有这个字符串:

I have it splitted in the comma(,) too. And if I have this String:

"That´s a problem here?123456"

它也会在´上拆分,那么我该如何解决?

It also splits on ´, So how can I fix this?

推荐答案

我没有看到这种行为:(我也不希望如此)

I am not seeing this behaviour: (nor would I expect to)

String s ="hello?1000";

String[] fields = s.split("\\?");

for (String field : fields) {
   System.out.println(field);
}

产量:

你好

1000

引入逗号,"或撇号'"对拆分没有任何影响:

Introducing a comma "," or an apostrophe "'" doesn't make any difference to the split:

String s ="he,llo?1000";

产量:

他,llo

1000

String s ="he'llo?1000";

产量:

他会

1000

如果输入字符串中有空格,则溢出也可以正常工作.我只能建议您的正则表达式不是您想的那样!

The spilt also works fine if you have any spaces in your input string. I can only suggest that your regex is not what you think it is!

这篇关于尝试使用String.split("\\?")时发生意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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