I have a string which contains a contiguous chunk of digits and then a contiguous chunk of characters. I need to split them into two parts (one integer part, and one string).
我有一个字符串,其中包含一个连续的数字块,然后是一个连续的字符块。我需要将它们分成两部分(一个整数部分和一个字符串)。
I tried using String.split("\\D", 1)
, but it is eating up first character. I checked all the String API and didn't find a suitable method.
我尝试使用String.split(“\\ D”,1),但它吃掉了第一个字符。我检查了所有的String API,但没有找到合适的方法。
Is there any method for doing this thing?
做这件事有什么方法吗?
2 个解决方案
#1
13
Use lookarounds: str.split("(?<=\\d)(?=\\D)")
使用lookarounds:str.split(“(?<= \\ d)(?= \\ D)”)
String[] parts = "123XYZ".split("(?<=\\d)(?=\\D)");
System.out.println(parts[0] + "-" + parts[1]);
// prints "123-XYZ"
\d
is the character class for digits; \D
is its negation. So this zero-matching assertion matches the position where the preceding character is a digit (?<=\d)
, and the following character is a non-digit (?=\D)
.
\ d是数字的字符类; \ D是它的否定。所以这个零匹配断言匹配前一个字符是数字的位置(?<= \ d),后面的字符是非数字(?= \ D)。
References
- regular-expressions.info/Lookarounds and Character Class
- regular-expressions.info/Lookarounds和Character Class
Related questions
- Java split is eating my characters.
- Java分裂正在吃我的角色。
- Is there a way to split strings with String.split() and include the delimiters?
- 有没有办法用String.split()分割字符串并包含分隔符?
Alternate solution using limited split
The following also works:
以下也有效:
String[] parts = "123XYZ".split("(?=\\D)", 2);
System.out.println(parts[0] + "-" + parts[1]);
This splits just before we see a non-digit. This is much closer to your original solution, except that since it doesn't actually match the non-digit character, it doesn't "eat it up". Also, it uses limit
of 2
, which is really what you want here.
这在我们看到非数字之前就分裂了。这更接近你的原始解决方案,除了因为它实际上不匹配非数字字符,它不会“吃掉它”。此外,它使用2的限制,这真的是你想要的。
API links
-
String.split(String regex, int limit)
- If the limit
n
is greater than zero then the pattern will be applied at mostn - 1
times, the array's length will be no greater thann
, and the array's last entry will contain all input beyond the last matched delimiter. - 如果限制n大于零,那么模式将最多应用n - 1次,数组的长度将不大于n,并且数组的最后一个条目将包含超出最后一个匹配分隔符的所有输入。
- If the limit
- String.split(String regex,int limit)如果限制n大于零,那么模式将最多应用n - 1次,数组的长度将不大于n,并且数组的最后一个条目将包含所有输入超出最后匹配的分隔符。
#2
3
There's always an old-fashioned way:
总有一种老式的方式:
private String[] split(String in) {
int indexOfFirstChar = 0;
for (char c : in.toCharArray()) {
if (Character.isDigit(c)) {
indexOfFirstChar++;
} else {
break;
}
}
return new String[]{in.substring(0,indexOfFirstChar), in.substring(indexOfFirstChar)};
}
(hope it works with digit-only or char-only Strings too - can't test it here - if not, take it as a general idea)
(希望它也适用于仅限数字或仅限字符串的字符串 - 无法在此处测试 - 如果不是,请将其作为一般概念)
#1
13
Use lookarounds: str.split("(?<=\\d)(?=\\D)")
使用lookarounds:str.split(“(?<= \\ d)(?= \\ D)”)
String[] parts = "123XYZ".split("(?<=\\d)(?=\\D)");
System.out.println(parts[0] + "-" + parts[1]);
// prints "123-XYZ"
\d
is the character class for digits; \D
is its negation. So this zero-matching assertion matches the position where the preceding character is a digit (?<=\d)
, and the following character is a non-digit (?=\D)
.
\ d是数字的字符类; \ D是它的否定。所以这个零匹配断言匹配前一个字符是数字的位置(?<= \ d),后面的字符是非数字(?= \ D)。
References
- regular-expressions.info/Lookarounds and Character Class
- regular-expressions.info/Lookarounds和Character Class
Related questions
- Java split is eating my characters.
- Java分裂正在吃我的角色。
- Is there a way to split strings with String.split() and include the delimiters?
- 有没有办法用String.split()分割字符串并包含分隔符?
Alternate solution using limited split
The following also works:
以下也有效:
String[] parts = "123XYZ".split("(?=\\D)", 2);
System.out.println(parts[0] + "-" + parts[1]);
This splits just before we see a non-digit. This is much closer to your original solution, except that since it doesn't actually match the non-digit character, it doesn't "eat it up". Also, it uses limit
of 2
, which is really what you want here.
这在我们看到非数字之前就分裂了。这更接近你的原始解决方案,除了因为它实际上不匹配非数字字符,它不会“吃掉它”。此外,它使用2的限制,这真的是你想要的。
API links
-
String.split(String regex, int limit)
- If the limit
n
is greater than zero then the pattern will be applied at mostn - 1
times, the array's length will be no greater thann
, and the array's last entry will contain all input beyond the last matched delimiter. - 如果限制n大于零,那么模式将最多应用n - 1次,数组的长度将不大于n,并且数组的最后一个条目将包含超出最后一个匹配分隔符的所有输入。
- If the limit
- String.split(String regex,int limit)如果限制n大于零,那么模式将最多应用n - 1次,数组的长度将不大于n,并且数组的最后一个条目将包含所有输入超出最后匹配的分隔符。
#2
3
There's always an old-fashioned way:
总有一种老式的方式:
private String[] split(String in) {
int indexOfFirstChar = 0;
for (char c : in.toCharArray()) {
if (Character.isDigit(c)) {
indexOfFirstChar++;
} else {
break;
}
}
return new String[]{in.substring(0,indexOfFirstChar), in.substring(indexOfFirstChar)};
}
(hope it works with digit-only or char-only Strings too - can't test it here - if not, take it as a general idea)
(希望它也适用于仅限数字或仅限字符串的字符串 - 无法在此处测试 - 如果不是,请将其作为一般概念)