Suppose there are 3 strings:
protein, starch, drink
Concatenating those, we could say what is for dinner:
Example:
val protein = "fish"
val starch = "chips"
val drink = "wine"
val dinner = protein + ", " + starch + ", " + drink
But what if something was missing, for example the protein, because my wife couldn’t catch anything. Then, we will have: ,chips, drink for dinner.
There is a slick way to concatenate the strings to optionally add the commas – I just don’t know what it is 😉. Does anyone have a nice idea?
I’m looking for something like:
val dinner = protein +[add a comma if protein is not lenth of zero] + starch .....
It’s just a fun exercise I’m doing, so now sweat if it can’t be done in some cool way. The reason that I’m trying to do the conditional concatenation in a single assignment, is because I’m using this type of thing a lot in XML and a nice solution will make things….. nicer.
When you say “it may be absent”, this entity’s type should be
Option[T]. Then,You would invoke it like this:
In case you absolutely want checking a string’s emptiness,