如何在 Java 中拆分字符串并保留分隔符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3777546/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:20:11  来源:igfitidea点击:

How can I split a string in Java and retain the delimiters?

java

提问by yegor256

I have this string (Java 1.5):

我有这个字符串(Java 1.5):

:alpha;beta:gamma;delta

I need to get an array:

我需要得到一个数组:

{":alpha", ";beta", ":gamma", ";delta"}

What is the most convenient way to do it in Java?

在 Java 中最方便的方法是什么?

回答by Bozho

str.split("(?=[:;])")

This will give you the desired array, only with an empty first item. And:

这将为您提供所需的数组,只有一个空的第一项。和:

str.split("(?=\b[:;])")

This will give the array without the empty first item.

这将给出没有空的第一项的数组。

  • The key here is the (?=X)which is a zero-width positive lookahead (non-capturing construct) (see regex pattern docs).
  • [:;]means "either ; or :"
  • \bis word-boundary - it's there in order not to consider the first :as delimiter (since it is the beginning of the sequence)
  • 这里的关键是(?=X)这是一个零宽度正前瞻(非捕获构造)(请参阅正则表达式模式文档)。
  • [:;]意思是“要么;要么:”
  • \b是词边界 - 它在那里是为了不将第一个:视为分隔符(因为它是序列的开头)

回答by Fabian Steeg

To keep the separators, you can use a StringTokenizer:

要保留分隔符,您可以使用StringTokenizer

new StringTokenizer(":alpha;beta:gamma;delta", ":;", true)

That would yield the separators as tokens.

这将产生作为标记的分隔符。

To have them as part of your tokens, you could use String#splitwith lookahead.

要将它们作为令牌的一部分,您可以使用String#splitwith lookahead

回答by Favonius

You can do this by simply using patterns and matcher class in java regx.

您可以通过在 java regx 中简单地使用模式和匹配器类来做到这一点。

    public static String[] mysplit(String text)
    {
     List<String> s = new ArrayList<String>();
     Matcher m = Pattern.compile("(:|;)\w+").matcher(text);
     while(m.find()) {
   s.add(m.group());
     }
     return s.toArray(new String[s.size()]);
    }

回答by Kanagavelu Sugumar

/**
 * @param list an empty String list. used for internal purpose. 
 * @param str  String which has to be processed.
 * @return Splited String Array with delimiters.
 */
public  String[] split(ArrayList<String> list, String str){
  for(int i = str.length()-1 ; i >=0 ; i--){
     if(!Character.isLetterOrDigit((str.charAt(i)))) {
        list.add(str.substring(i, str.length()));
        split(list,str.substring(0,i));
        break;
     }
  }
  return list.toArray(new String[list.size()]);
}

回答by Rok T.

This should work with Java 1.5 (Pattern.quote was introduced in Java 1.5).

这应该适用于 Java 1.5(Pattern.quote 是在 Java 1.5 中引入的)。

// Split the string on delimiter, but don't delete the delimiter
private String[] splitStringOnDelimiter(String text, String delimiter, String safeSequence){
    // A temporary delimiter must be added as Java split method deletes the delimiter

    // for safeSequence use something that doesn't occur in your texts 
    text=text.replaceAll(Pattern.quote(delimiter), safeSequence+delimiter);
    return text.split(Pattern.quote(safeSequence));
}

If first element is the problem:

如果第一个元素是问题:

private String[] splitStringOnDelimiter(String text, String delimiter, String safeSequence){
    text=text.replaceAll(Pattern.quote(delimiter), safeSequence+delimiter);
    String[] tempArray = text.split(Pattern.quote(safeSequence));
    String[] returnArray = new String[tempArray.length-1];
    System.arraycopy(tempArray, 1, returnArray, 0, returnArray.length);
    return returnArray;
}

E.g., here "a" is the delimiter:

例如,这里的“a”是分隔符:

splitStringOnDelimiter("-asd-asd-g----10-9asdas jadd", "a", "<>")

You get this:

你得到这个:

1.: -
2.: asd-
3.: asd-g----10-9
4.: asd
5.: as j
6.: add

If you in fact want this:

如果你真的想要这个:

1.: -a
2.: sd-a
3.: sd-g----10-9a
4.: sda
5.: s ja
6.: dd

You switch:

你切换:

safeSequence+delimiter

with

delimiter+safeSequence

回答by chillysapien

Assuming that you only have a finite set of seperators before the words in your string (eg ;, : etc) you can use the following technique. (apologies for any syntax errors, but its been a while since I used Java)

假设您的字符串中的单词之前只有一组有限的分隔符(例如;、: 等),您可以使用以下技术。(对任何语法错误表示歉意,但自从我使用 Java 以来已经有一段时间了)

String toSplit = ":alpha;beta:gamma;delta "
toSplit = toSplit.replace(":", "~:")
toSplit = toSplit.replace(";", "~;")
//repeat for all you possible seperators
String[] splitStrings = toSplit.split("~")