Skip to content
Advertisement

How to append in String Builder in java? [closed]

Suppose I have sentence in String X. How can I split each word in sentence and add a span tag for each word with class word and if the word matches with String y add class word selected?

Both X and y Are dynamic

JavaScript

I create a function and split into array , but i am not able to append to string builder

JavaScript

My output should look like this

JavaScript

Advertisement

Answer

I’d split x by space, stream it, wrap every word with its span, and then join it back. For the distinction between selected and non selected words, I’d split y, store the words in a Set (not strictly required, but should improve performance, especially on large inputs), and use that to check for the required class:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement