I haven’t found any solution to a specific problem I’m having. I’m doing some XML parsing in which I would like to manipulate words within a string.
Given I have a case of: 'word, word word word' – words can be any string, as I have no knowledge what they would be in advance.
I’d like to be able to manipulate the string to obtain the following outcome: 'word., word word. word' – So, the first word and the third word I would like to append a '.' to the end of them.
What would be a suitable approach for this? Would regex be the best way?
Assuming that words will be separated using the same character(s), I think that the simplest way would be to use
indexOf(" ")and then usesubstringmethods to add a dot before the returned index. Remember that you could also uselastIndexOf(" ")for appending the dot to the third word, or you could even specify the starting position ofindexOfi.e(I think something along those lines would work, but I haven’t tested and I wrote it without an IDE)
Of course, I’m not very familiar with regex and you haven’t really given that much information, so regex could potentially be an option, but I think that would require you to know the length(s) of the word(s), at the least.