1

I have a very long string consisting of words punctuation and spaces. I am trying to modify the string to add a new line (\n) after every x characters but while preserving whole words. For example:

An evil human sprayed innocent students in the face with pepper spray

With x = 20 would output like this:

An evil human sprayed 21 chars innocent students in 20 chars the face with pepper 20 chars spray end of string 

If words would be split by inserting a \n after x characters, then the word should be allowed to stay on the line, while a new line would be inserted at the end of the word.

Is there a catch-all way of doing this without a whole lot of complicated code?

2 Answers 2

3

Take a look at the Apache commons text library, in particular WordUtils#wrap().

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing me in Apache's direction. I didn't know about that package and it seems very powerful. However, for this implementation I would prefer to opt for a regex-like answer like @ruakh's (+1)
2

I would write:

s = s.replaceAll("(.{20,}?)\\s+", "$1\n"); 

This will find every instance of at least twenty non-newline characters, followed by one or more whitespace characters, and replace the whitespace with a newline. Or, to make x decidable at run-time (rather than hard-coded as 20), I'd write:

s = s.replaceAll("(.{" + x + ",}?)\\s+", "$1\n"); 

1 Comment

This is the kind of answer I was hoping for. I'm going to give a quick test run and try to break it.