Geek February 2016

How to use StringTokenizer properly

How should I use StringTokenizer so that when delimiters appear consecutively several times, StringTokenizer would recognize the empty space between them as an empty String and print empty line?

Alternatively, how can I use String.split() method that takes several characters as delimiters and does the same job?

For example String "abcd;,;ef,,gh" should be printed as:

abcd  
# empty line here
# empty line here
ef
# empty line here
gh

Answers


Juan Carlos Alvarez March 2016

As per your input

"abcd;,;ef,,gh"

Output:

abcd  
# empty line here
# empty line here
ef
# empty line here
gh

Why there are only 2 "empty line here" , when there was ;,; ?

Maybe you can replace all the "delimiters" with whitespaces, tokenize then print each string with a foreach, and inside the loop print a whitespace or "empty line here" ?

Post Status

Asked in February 2016
Viewed 2,016 times
Voted 10
Answered 1 times

Search




Leave an answer