Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

StringTokenizer tab delimiter  RSS feed

 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
How can i use StringTokenizer delimiter as "tab"
thanks
jowskai
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use "\t" as the delimiter, like:
 
abani patra
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have test "\t" as delimiter but that do not work
Any help?
 
Harsha Smith
Ranch Hand
Posts: 287
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Blake. This code works fine for me:



Output:

Huey
Louis
Dewey
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interestingly, "\\t" also works fine.

 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"\t" is a single tab character. "\\t" means the regular expression "\t" which also is a single tab character. Slight difference, same result.
 
dhaval tamboli
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for others to make it work.. use one space before \t.
e.g.

 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for others to make it work.. use one space before \t.

No, don't do this for the following reasons:

1. It isn't necessary. If you want to split on a tab then specifying \t works fine.
2. It's wrong. It will change the way the string splits from just splitting on tabs to splitting on both tabs and whitespace.

If you don't believe me run this code.


BTW you shouldn't use StringTokenizer, the API docs say "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."
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!