如何根据存储在列表中的多个分隔符拆分字符串?

huangapple go评论67阅读模式
英文:

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&lt;String&gt; delimiters= Arrays.asList(&quot;,&quot;,&quot;.&quot;);

If I have a string like &quot;a.b,c&quot;.

How to split on all the delimiters?

答案1

得分: 2

可以使用正则表达式。

@Test
public void test() {
List 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());

}

英文:

You can do a regular expression.

@Test
public void test() {
    List&lt;String&gt; delimiters = Arrays.asList(&quot;,&quot;, &quot;.&quot;, &quot;?&quot;);
    String str = &quot;a.b,c?dr&quot;;

    StringBuilder regex = new StringBuilder();
    regex.append(&quot;[&quot;);
    delimiters.stream().forEach(delimiter -&gt; regex.append(delimiter + &quot;/&quot;));
    regex.append(&quot;]&quot;);

    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&lt;String&gt; delimiters = Arrays.asList(&quot;,&quot;, &quot;.&quot;, &quot;?&quot;, &quot;:&quot;);

for (String delimiter : delimiters) {
  text = text.replace(delimiter, &quot; &quot;); //replace all delimiter occurrence with space
}

String[] output = text.split(&quot; &quot;); //split using space

答案3

得分: 0

String s = "a.b,c";
String[] split = s.split("[.|,]");

这样怎么样?使用“|”来分隔多个分隔符,记得要加上[],否则就需要进行转义。

英文:
    String s = &quot;a.b,c&quot;;
    String[] split = s.split(&quot;[.|,]&quot;);

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&lt;String&gt; delimiters= Arrays.asList(&quot;,&quot;,&quot;.&quot;,&quot;!&quot;,&quot;?&quot;,&quot;$&quot;,&quot;&amp;&quot;,&quot;^&quot;,&quot;%&quot;,&quot;@&quot;);
		String givenString = &quot;Test,String.with?Multiple@delimeters!example&quot;;
		//Create Regex to split sting
		StringBuilder regex = new StringBuilder();
		regex.append(&quot;[&quot;);
		delimiters.forEach(delimeter -&gt; regex.append(delimeter));
		regex.append(&quot;]&quot;);
		String[] tokens = givenString.split(regex.toString());
		System.out.println(&quot;Number of tokens = &quot; + tokens.length);
		for(int i =0; i&lt;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(&quot;[\\.]&quot;, &quot;,&quot;).split(&quot;,&quot;);

huangapple
  • 本文由 发表于 2020年5月29日 14:48:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/62080211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定