在单个空格处拆分字符串

时间:2022-08-22 12:50:23

I am looking to split a string into an array of strings at every single space.

我希望在每个空格中将字符串拆分为一个字符串数组。

For example:

This_sentence_gets__split. (Underscores represent spaces here)

This_sentence_gets__split。 (下划线代表空格)

Becomes

"This"

"sentence"

"gets"

" " (note the double space was left as one space)

“”(注意双空格留作一个空格)

"split."

I assume I can do this with String.split(String regex) but I am not very good with regular expressions and I do not know how I would accomplish this.

我假设我可以使用String.split(String regex)执行此操作,但我对正则表达式不是很好,我不知道如何实现这一点。

Edit:

Any spaces after the first should be split into substrings as well. For example: Split___this. becomes "Split " " " " "this."

第一个之后的任何空格也应该分成子串。例如:Split___this。变得“分裂”“”“”这个。“

4 个解决方案

#1


7  

I think this might be what you are looking for (you can improve it with \\s class for all whitespace like tabs, new lines and so on)

我认为这可能是你正在寻找的东西(你可以使用\\ s类改进它,用于所有空格,如制表符,新行等)

String data = "This_sentence__gets___split".replace('_', ' ');
// System.out.println(data);
String arr[] = data.split("(?<! ) |(?<= {2})");
for (String s : arr)
    System.out.println("\"" + s + "\"");

Output:

"This"
"sentence"
" "
"gets"
" "
" "
"split."

Explanation:

  • "(?<! ) " will split only on spaces that don't have space before it
  • “(?

  • "(?<= {2})" will split in place that have two spaces before it.
  • “(?<= {2})”将在它之前分成两个空格。

#2


1  

Its not that difficult:

它并不困难:

str.split(" ");

#3


0  

I'm not super knowledgeable on regular expressions either, which is why I choose to use Google Guava's Splitter class.

我对正则表达式也不是很了解,这就是我选择使用Google Guava的Splitter类的原因。

Example:

private static final Splitter splitter = 
      Splitter.on(',').trimResults().omitEmptyStrings();

//results in {"some", "string"}
splitter.split("some string");

You can play around with trimResults(), omitEmptyStrings(), and other options to tweak it how you like.

您可以使用trimResults(),omitEmptyStrings()和其他选项来调整它的喜好。

:)

#4


0  

Try StringTokenizer.

 StringTokenizer st = new StringTokenizer("this is a test", " ", true);
 while (st.hasMoreTokens()) {
     System.out.println(st.nextToken());
 }

UPDATE:

The third parameter (true) will treat the delimiters as separate tokens, so that they will be returned separately.

第三个参数(true)会将分隔符视为单独的标记,以便它们将单独返回。

#1


7  

I think this might be what you are looking for (you can improve it with \\s class for all whitespace like tabs, new lines and so on)

我认为这可能是你正在寻找的东西(你可以使用\\ s类改进它,用于所有空格,如制表符,新行等)

String data = "This_sentence__gets___split".replace('_', ' ');
// System.out.println(data);
String arr[] = data.split("(?<! ) |(?<= {2})");
for (String s : arr)
    System.out.println("\"" + s + "\"");

Output:

"This"
"sentence"
" "
"gets"
" "
" "
"split."

Explanation:

  • "(?<! ) " will split only on spaces that don't have space before it
  • “(?

  • "(?<= {2})" will split in place that have two spaces before it.
  • “(?<= {2})”将在它之前分成两个空格。

#2


1  

Its not that difficult:

它并不困难:

str.split(" ");

#3


0  

I'm not super knowledgeable on regular expressions either, which is why I choose to use Google Guava's Splitter class.

我对正则表达式也不是很了解,这就是我选择使用Google Guava的Splitter类的原因。

Example:

private static final Splitter splitter = 
      Splitter.on(',').trimResults().omitEmptyStrings();

//results in {"some", "string"}
splitter.split("some string");

You can play around with trimResults(), omitEmptyStrings(), and other options to tweak it how you like.

您可以使用trimResults(),omitEmptyStrings()和其他选项来调整它的喜好。

:)

#4


0  

Try StringTokenizer.

 StringTokenizer st = new StringTokenizer("this is a test", " ", true);
 while (st.hasMoreTokens()) {
     System.out.println(st.nextToken());
 }

UPDATE:

The third parameter (true) will treat the delimiters as separate tokens, so that they will be returned separately.

第三个参数(true)会将分隔符视为单独的标记,以便它们将单独返回。