Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help.... Read data in between

 
Huang Qingyan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Tiger,
It is so nice to hear from you again. Thanks for the trouble of your source code. Can I ask you another question again?
My question is how do I read the text data between two characters or two String, for example:
@HelloWorld@
test_HelloWorld_test
The data I wanted is of course the text "HelloWorld", but how can I do it? StringTokenizer class cannot do this kind of stuff.
Can you please kindly advise me on this?
Warmest regards,
Franco
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Huang
Why won't the StringTokenizer work?

If it is possible for the tokens to be part of the string then you're right it wont work but then the only way to get what you need is to know the postions of the tokens and then pull a subsring from the original.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks like a fairly standard templating problem to me. I guess you have these sort of token groups scattered at random throughout a file or stream, and waht to substitute different values for different tokens.
You can use StringTokenizer, but you need to keep track of whether you are "inside" or "outside" a token, even in tricky cases like "xx@name1@@name2@yy". The answer is to note that StringTokenizer has an alternative constructor which takes an extra parameter indicating whether to return the separators as well as the separated text:
StringTokenizer(String s, String delim, boolean returnDelims)
If you use this constructor, with the extra parameter set to true, you can do something like:

Has this helped ?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another strategy I'd probably consider using would involve regular expressions (java.util.regex).
 
Huang Qingyan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I mean is there any other alternatives rather than using StringTokenizer()?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dirk Schreckmann:
Another strategy I'd probably consider using would involve regular expressions (java.util.regex).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic