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)
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.
Edit:
Any spaces after the first should be split into substrings as well. For example:
Split___this. becomes "Split " " " " "this."
I think this might be what you are looking for (you can improve it with
\\sclass for all whitespace like tabs, new lines and so on)Output:
Explanation:
"(?<! ) "will split only on spaces that don’t have space before it"(?<= {2})"will split in place that have two spaces before it.