I have a situation where I must rearrange a string into an array for a unique user display. Whenever encountering an escape character (such as \n
for a new line), it needs to be added as a separate item in the array.
For example, if the string is:
sampe\nanother sample HERE\n\n\nfinal
It should be formatted as:
["sampe", "another sample HERE", "", "", "final"]
In this case, the empty strings represent the new lines within the array. How can this be accomplished?
UPDATE: There is a special condition where any content enclosed between ${
and }
should also be considered as a separate array element.
For instance:
"sampe\n${abc}another sample HERE\n\n\nfinal"
In such a scenario, the array should appear as:
["sampale", "${abc}", "another sample HERE", "", "", "final"]
Therefore, this functionality should also be attainable. Currently, without taking into account the escape characters, I am iterating through the array to identify occurrences of ${
and }
. How can this be achieved while considering the \n
escape character?