mercredi 25 février 2015

Having trouble splitting words in java

I have the following string:



String text = "stack Overflow, stack"


a char:



char c = "*"


and another string:



String word = "Overflow"


the whole idea is to check for the word "overflow" in the text string, and surround it with the char c. The output should look something like this:



"stack *Overflow*, stack"


First, in order to solve this, i thought of splitting the string into words, and then check for each word if it contains the desired string as a substring of itself. Then create a stringBuilder, and append the char on each end of the word. I get no erros, but a wrong output though:



"stack *Overflow,* stack


Any help on how to solve this properly ?


Aucun commentaire:

Enregistrer un commentaire