Java string - replace multiple spaces by one and t

2020-04-16 06:22发布

问题:

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 6 years ago.

Suppose I have a string

s ="[    2323.2323 232.3232 0.2 0.3 3232]"

I want to split it into an array of strings by space delimiter. I also want to preserve a single space between values and ignore the multiple space.

What would be the regular expression to do this?

回答1:

First get rid of multiple spaces:

String after = before.trim().replaceAll(" +", " ");

Then Split the String up into your array using the split method