如何在 Java 中使用多个分隔符拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14305593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to split a String with multiple delimiters in Java
提问by Venzentx
I am trying to have the desired outputs like this:
我正在尝试像这样获得所需的输出:
555
555
5555
by using codes:
通过使用代码:
public class Split {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String phoneNumber = "(555) 555-5555";
String[] splitNumberParts = phoneNumber.split(" |-");
for(String part : splitNumberParts)
System.out.println(part);
But dont know how to get rid of the "( )" from the first element.
但不知道如何从第一个元素中去掉“( )”。
Thanks in advance.
提前致谢。
Regards
问候
回答by Audrius Meskauskas
StringTokenizersupports multiple delimiters that can be listed in a string as second parameter.
StringTokenizer支持多个分隔符,这些分隔符可以作为第二个参数列在字符串中。
StringTokenizer tok = new StringTokenizer(phoneNumber, "()- ");
String a = tok.nextToken();
String b = tok.nextToken();
String c = tok.nextToken();
In your case, this will give a = 555, b = 555, c = 5555.
在您的情况下,这将给出 a = 555,b = 555,c = 5555。
回答by Will
why not do a replace first?
为什么不先更换?
String phoneNumber = "(555) 555-5555";
String cleaned = phoneNumber.replaceAll("[^0-9]",""); // "5555555555"
Then you can use substring to get the parts you want.
然后你可以使用 substring 来获取你想要的部分。
回答by Rohit Jain
If you just want your digits in sequence, then you can probably make use of Patternand Matcherclass, to find all the substrings matching a particular pattern, in your case, \d+
.
如果您只想按顺序排列数字,那么您可以使用Pattern和Matcher类来查找与特定模式匹配的所有子字符串,在您的情况下,\d+
.
You would need to use Matcher#find()
method to get all the substrings matching your pattern. And use Matcher#group()
to print the substring matched.
您需要使用Matcher#find()
method 来获取与您的模式匹配的所有子字符串。并用于Matcher#group()
打印匹配的子字符串。
Here's how you do it: -
这是您的操作方法:-
String phoneNumber = "(555) 555-5555";
// Create a Pattern object for your required Regex pattern
Pattern pattern = Pattern.compile("\d+");
// Create a Matcher object for matching the above pattern in your string
Matcher matcher = pattern.matcher(phoneNumber);
// Use Matcher#find method to fetch all matching pattern.
while (matcher.find()) {
System.out.println(matcher.group());
}
回答by Daniel Figueroa
When you're using split java will return an array of string which is separated from where there was an occurence of the items which you provided, thus if you where to do a split like you are doing now, you could add something like this:
当您使用 split java 时,将返回一个字符串数组,该数组与您提供的项目出现的位置分开,因此如果您像现在这样进行拆分,您可以添加如下内容:
phoneNumber.split(" |-|\(|\) ");
But thats not very nice looking, now is it? So instead we can do something like this, which basically tells java to remove anything that isn't a number:
但那不是很好看,现在是吗?所以我们可以做这样的事情,它基本上告诉java删除任何不是数字的东西:
String[] splitNumberParts = phoneNumber.split("\D+");