Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate string by whitespace, but keep newlines in split array

I'm trying to split a string in Java, but keep the newline characters as elements in the array.

For example, with input: "Hello \n\n\nworld!"

I want the output to be: ["Hello", "\n", "\n", "\n", "world", "!"]

The regex I have in place right now is this:
String[] parsed = input.split(" +|(?=\\p{Punct})|(?<=\\p{Punct})");

This gets me the punctuation separation I want, but its output looks like this:
["Hello", "\n\n\nworld", "!"]

Is there a way to unclump the newlines in Java?

like image 567
Alynchos Avatar asked Jul 03 '26 00:07

Alynchos


1 Answers

You could first replace all \n with \n (newline and a space) and then do a simple split on the space character.

    String input = "Hello \n\n\nworld!";
    String replacement = input.replace("\n", "\n ");
    String[] result = replacement.split(" ");
  • input: "Hello \n\n\nworld!"
  • replacement: "Hello \n \n \n world!"
  • result: ["Hello", "\n", "\n", "\n", "world!"]

Note: my example does not handle the final exclamation mark - but it seems you already know how to handle that.

like image 89
TmTron Avatar answered Jul 04 '26 15:07

TmTron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!