• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String tokenizer

 
Kalpesh Soni
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

output
------
C:\jdk\bin\java.exe TokenTest
Working Directory - D:\ksoni\javaTests\
Class Path - .;c:\jdk\lib;c:\j2sdkee1.3\lib\j2ee.jar;c:\KawaEnt5.0\kawaclasses.zip;c:\jdk\lib\tools.jar;c:\jdk\jre\lib\rt.jar;c:\jdk\jre\lib\i18n.jar
1[1003]
2[$]
3[$]
4[03]
5[$]
6[$]
7[$]
8[$]
9[suggestion1]

1[1003]
2[03]
3[suggestion1]
Process Exit...
--------
question
--------
how do i get an array of tokens ?
i do not want the $ sign
the token may not be there !in that case array element should be null
and see the delim "$a" , it seems a is ignored !
what if i have delim of two chars or more ??
--kalpesh
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gidday!
In the case of your delimiters, JAVA treats every character in your delimiter string as a separate delimiter. This means that '$' and 'a' are delimiters, but not "$a".
To get rid of the $-sign in your tokens, use the second one of your StringTokenizers, eg the one without true in the constructor. Here, true means that the delimiters are also returned as tokens (as you probably noticed)...
You can't have delimiters that are not chars.
If you want to store the tokens, consider whether you know for sure how many tokens the string will result in. If you do, use an ordinary array of String. If you don't, you might want to use a Vector or some other dynamic storage...
Hope this helps!
//Kaspar
 
Kalpesh Soni
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could not know how to use String Tokenizer to get string array
i used good old indexof

--output--
C:\jdk\bin\java.exe StringToArray
Working Directory - D:\ksoni\javaTests\
Class Path - .;c:\jdk\lib;c:\j2sdkee1.3\lib\j2ee.jar;c:\KawaEnt5.0\kawaclasses.zip;c:\jdk\lib\tools.jar;c:\jdk\jre\lib\rt.jar;c:\jdk\jre\lib\i18n.jar;C:\jdk\lib
1 index[i-1] 4 index[i]5
2 index[i-1] 5 index[i]8
3 index[i-1] 8 index[i]9
4 index[i-1] 9 index[i]10
5 index[i-1] 10 index[i]11
6 index[i-1] 11 index[i]-1
7 index[i-1] -1 index[i]-1
8 index[i-1] -1 index[i]26
[1003]
null 2![]
[03]
null 2![]
null 2![]
null 2![]
[suggestion1 tp]
null ![null]
null ![null]

1003$$03$$$$suggestion1 tp
Process Exit...

------------------
"Sun Certified Java Programmer"
KS
"Failing to plan is like plannig to fail!"
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic