This should be really simple. I'm searching for the cleanest & shortest way to add a newline to a non-null String if it is not empty, i.e.
true. I know of a number of ways to do this, but none of them are as elegant as I would like them to.
For context, the string I'm working on will essentially be a list of key-value pairs that is filled with a loop. Every key-value-pair needs to have a newline appended after it, though not if it is the last one. An example for the end value of
myString might be the following:
Boss : Julia Worker : Louis Intern : Tom
I could do
myString += myString.isEmpty() ? "" : "\n";
but is this really the best way to do this?
I also thought of
if(!myString.isEmpty()) myString += "\n";
Which way would be preferable, and why? Are there better alternatives?