英文:
How to split a string on multiple delimiters stored in a list?
问题
我有一个分隔符列表,类似于
List<String> delimiters = Arrays.asList(",", ".");
如果我有一个字符串,像是 a.b,c
。
如何根据所有的分隔符进行分割?
英文:
I have a list of delimiters like
List<String> delimiters= Arrays.asList(",",".");
If I have a string like "a.b,c"
.
How to split on all the delimiters?
答案1
得分: 2
可以使用正则表达式。
@Test
public void test() {
List
String str = "a.b,c?dr";
StringBuilder regex = new StringBuilder();
regex.append("[");
delimiters.stream().forEach(delimiter -> regex.append(delimiter + "/"));
regex.append("]");
String[] result = str.split(regex.toString());
}
英文:
You can do a regular expression.
@Test
public void test() {
List<String> delimiters = Arrays.asList(",", ".", "?");
String str = "a.b,c?dr";
StringBuilder regex = new StringBuilder();
regex.append("[");
delimiters.stream().forEach(delimiter -> regex.append(delimiter + "/"));
regex.append("]");
String[] result = str.split(regex.toString());
}
答案2
得分: 2
请检查以下代码是否解决了您的问题:
List<String> delimiters = Arrays.asList(",", ".", "?", ":");
for (String delimiter : delimiters) {
text = text.replace(delimiter, " "); //将所有分隔符出现替换为空格
}
String[] output = text.split(" "); //使用空格进行分割
英文:
Please check below code whether it solves your problem:
List<String> delimiters = Arrays.asList(",", ".", "?", ":");
for (String delimiter : delimiters) {
text = text.replace(delimiter, " "); //replace all delimiter occurrence with space
}
String[] output = text.split(" "); //split using space
答案3
得分: 0
String s = "a.b,c";
String[] split = s.split("[.|,]");
这样怎么样?使用“|”来分隔多个分隔符,记得要加上[],否则就需要进行转义。
英文:
String s = "a.b,c";
String[] split = s.split("[.|,]");
how about this? Use “|” to separate multiple delimiters, remember to add [], Otherwise it needs to be escaped
答案4
得分: 0
这是代码:
import java.util.Arrays;
import java.util.List;
public class StringSplitMultipleDelimeter {
public static void main(String[] args) {
List<String> delimiters = Arrays.asList(",", ".", "!", "?", "$", "&", "^", "%", "@");
String givenString = "Test,String.with?Multiple@delimeters!example";
// 创建正则表达式以分割字符串
StringBuilder regex = new StringBuilder();
regex.append("[");
delimiters.forEach(delimiter -> regex.append(delimiter));
regex.append("]");
String[] tokens = givenString.split(regex.toString());
System.out.println("Token 数量 = " + tokens.length);
for (int i = 0; i < tokens.length; i++) {
System.out.println(tokens[i]);
}
}
}
英文:
Here is code:
import java.util.Arrays;
import java.util.List;
public class StringSplitMultipleDelimeter {
public static void main(String[] args) {
List<String> delimiters= Arrays.asList(",",".","!","?","$","&","^","%","@");
String givenString = "Test,String.with?Multiple@delimeters!example";
//Create Regex to split sting
StringBuilder regex = new StringBuilder();
regex.append("[");
delimiters.forEach(delimeter -> regex.append(delimeter));
regex.append("]");
String[] tokens = givenString.split(regex.toString());
System.out.println("Number of tokens = " + tokens.length);
for(int i =0; i<tokens.length; i++) {
System.out.println(tokens[i]);
}
}
}
答案5
得分: -1
你可以先在字符串上使用 replaceAll
,将其全部替换为一个固定的分隔符(您选择的分隔符),像这样 - 然后您可以基于该分隔符进行拆分:
String[] newArray = stringArray.replaceAll("[\\.]", ",").split(",");
英文:
You can use replaceAll on the string first and convert it all to a fixed delimiter (your choice) like this - then you can split it based on that:
String[] newArray = stringArray.replaceAll("[\\.]", ",").split(",");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论