This content has been marked as final. Show 4 replies
user8411781 wrote:Do you have any particular reason this has to be done using StringTokenizer?
I want to divide the string by using StringTokenizer and after modifying a particular token I again want to build the String as it before.
It seems it would be much simpler to do it with [url http://download.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29]String.replaceAll()
1 person found this helpful
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.That said if you have a single delimiter just put them all together again with a StringBuilder while inserting the delimiter between the tokens.
Thanks. You are right.
You could construct your StringTokenizer with returnDelims set to true. In that way delimiters count as tokens and are returned by nextToken(), so you can re-build your string.
The downside is that you will have to distinguish delimiters from regular tokens when processing the sequence of tokens.