如何将字符串分割为两个分隔符?

时间:2022-06-25 02:49:16

I know that you can split your string using myString.split("something"). But I do not know how I can split a string by two delimiters.

我知道你可以用myString.split("something")来分割你的字符串。但是我不知道如何用两个分隔符分割一个字符串。

Example:

例子:

mySring = "abc==abc++abc==bc++abc";

I need something like this:

我需要这样的东西:

myString.split("==|++")

What is its regularExpression?

它的regularExpression是什么?

4 个解决方案

#1


28  

Use this :

用这个:

 myString.split("(==)|(\\+\\+)")

#2


7  

How I would do it if I had to split using two substrings:

如果我必须使用两个子字符串进行分割,我该如何做:

String mainString = "This is a dummy string with both_spaces_and_underscores!"
String delimiter1 = " ";
String delimiter2 = "_";
mainString = mainString.replaceAll(delimiter2, delimiter1);
String[] split_string = mainString.split(delimiter1);

Replace all instances of second delimiter with first and split with first.

用first替换第二个分隔符的所有实例,并用first拆分。

Note: using replaceAll allows you to use regexp for delimiter2. So, you should actually replace all matches of delimiter2 with some string that matches delimiter1's regexp.

注意:使用replaceAll允许您对delimiter2使用regexp。因此,实际上应该用匹配delimiter1的regexp的字符串替换delimiter2的所有匹配。

#3


6  

You can use this

你可以使用这个

        mySring = "abc==abc++abc==bc++abc";
        String[] splitString = myString.split("\\W+");

Regular expression \W+ ---> it will split the string based upon non-word character.

正则表达式\W+ ->它将基于非单词字符拆分字符串。

#4


5  

Try this

试试这个

String str = "aa==bb++cc";
String[] split = str.split("={2}|\\+{2}");
System.out.println(Arrays.toString(split));

The answer is an array of

答案是一系列的。

[aa, bb, cc]

The {2} matches two characters of the proceding character. That is either = or + (escaped) The | matches either side

{2}匹配正在处理的字符的两个字符。这是=或+(转义)|匹配任何一方

I am escaping the \ in java so the regex is actually ={2}|\+{2}

我在java中转义,所以regex实际上= {2}|\ {2}

#1


28  

Use this :

用这个:

 myString.split("(==)|(\\+\\+)")

#2


7  

How I would do it if I had to split using two substrings:

如果我必须使用两个子字符串进行分割,我该如何做:

String mainString = "This is a dummy string with both_spaces_and_underscores!"
String delimiter1 = " ";
String delimiter2 = "_";
mainString = mainString.replaceAll(delimiter2, delimiter1);
String[] split_string = mainString.split(delimiter1);

Replace all instances of second delimiter with first and split with first.

用first替换第二个分隔符的所有实例,并用first拆分。

Note: using replaceAll allows you to use regexp for delimiter2. So, you should actually replace all matches of delimiter2 with some string that matches delimiter1's regexp.

注意:使用replaceAll允许您对delimiter2使用regexp。因此,实际上应该用匹配delimiter1的regexp的字符串替换delimiter2的所有匹配。

#3


6  

You can use this

你可以使用这个

        mySring = "abc==abc++abc==bc++abc";
        String[] splitString = myString.split("\\W+");

Regular expression \W+ ---> it will split the string based upon non-word character.

正则表达式\W+ ->它将基于非单词字符拆分字符串。

#4


5  

Try this

试试这个

String str = "aa==bb++cc";
String[] split = str.split("={2}|\\+{2}");
System.out.println(Arrays.toString(split));

The answer is an array of

答案是一系列的。

[aa, bb, cc]

The {2} matches two characters of the proceding character. That is either = or + (escaped) The | matches either side

{2}匹配正在处理的字符的两个字符。这是=或+(转义)|匹配任何一方

I am escaping the \ in java so the regex is actually ={2}|\+{2}

我在java中转义,所以regex实际上= {2}|\ {2}