简体   繁体   English

Java字符串拆分返回空结果

[英]Java string split returns empty results

Given input is always going to start with 0 and then follow as: c1 + occurrence where c1 is the character and occurrence is the sequence of the same character repeating. 给定输入总是从0开始,然后为:c1 +出现,其中c1是字符,出现是相同字符重复的序列。 For example aabbaaacccc becomes 0a2b2a3c4 , characters will be always lowercase az. 例如aabbaaacccc变为0a2b2a3c4 ,字符将始终为小写字母az。

Now my issue is given input as: 0x1k1c4t11g3d1m1d1j10f1v1n3e2r3i1e2a1h4a2e1y1z2e1s1a1q1j2r1k2t3h1i1f4j1d2m4p3 However when I use String.split() and iterate through the results I am getting empty strings. 现在我的问题输入为: 0x1k1c4t11g3d1m1d1j10f1v1n3e2r3i1e2a1h4a2e1y1z2e1s1a1q1j2r1k2t3h1i1f4j1d2m4p3但是,当我使用String.split()和iterate结果时,得到了结果。 I tried using both split("[0-9]") and split("[^az]") but result does not change. 我尝试同时使用split("[0-9]")split("[^az]")但结果没有改变。

The iteration result for my example is: 我的示例的迭代结果是:

x
k
c
t

g
d
m
d
j

f
v
n
e
r
i
e
a
h
a
e
y
z
e
s
a
q
j
r
k
t
h
i
f
j
d
m
p

Is this a bug in JDK or is there something wrong with my regex? 这是JDK中的错误,还是我的正则表达式有问题?

The problem here seems to be that you split by a regex that's is of exactly one number or char, you have an empty string when you get a two digit number for example t11 you get t1 and you loose the final 1, if you want to take the whole number you need to put + after what the regex is looking for, for example in this case you should put. 这里的问题似乎是由正则表达式拆分的,正则表达式正好是一个数字或char,当您获得两位数的数字时,例如t11,您得到一个t1,然后松开最后一个1,如果您想取正则表达式要查找的内容之后需要加+的整数,例如,在这种情况下,您应该加。 split("[0-9]+") and you would get the whole numbers no matter how many digits you have. split(“ [0-9] +”),无论有多少位数,您都将得到整数。

As Vulcan states in his comment, this is caused due to the existence of at least two consecutive numbers which result in empty String. 正如Vulcan在他的评论中指出的那样,这是由于存在至少两个连续的数字而导致空String引起的。 Maybe you would like to remove any numbers from the String first or obviously, remove the empty Strings in your resulted array. 也许您想先从字符串中删除任何数字,或者显然是从结果数组中删除空字符串。 for instance: 例如:

s = "0x1k1c4t11g3d1m1d1j10f1v1n3e2r3i1e2a1h4a2e1y1z2e1s1a1q1j2r1k2t3h1i1f4j1d2m4p3";    
s = s.replaceAll("\\d","");

I don't think it is good idea to use String split() in this case. 我不认为在这种情况下使用String split()是个好主意。 it will be better if you use substring and charAt() in this case, then try to consider a loop that is going to count number of occurrences. 如果在这种情况下使用substringcharAt()会更好,然后尝试考虑将要计算出现次数的循环。

You need to split by a zero-length pattern. 您需要按零长度模式进行分割。 A lookahead assertion , which is non-capturing, is the way to go: 不捕获的前瞻性断言是解决方法:

String str = "0d1j10f1";
str.split("(?=[a-z])");
// result: ["0", "d1", "j10", "f1"]


Also, as pointed out in other answers, keep in mind numbers can be multiple-digit. 同样,如其他答案所指出的,请记住数字可以是多位数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM