• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parsing and getting a value from a String

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any help would be appreciated. I am learning Java. I have a string (see below). I need to parse out the number of documents (in this case there are 454). This number may change so the only static text on here is "Advanced..." and "Document(s)". How can I parse out just that number?

String alltext2 = (String)Table_HtmlTable_0().getProperty(".text");
System.out.println("AllText2: " + alltext2);

AllText2: Employee Revenue � � � Corporate Categories >�Employee Revenue �Search� Advanced... 454 Document(s)�in�Employee Revenue �� �View Details � �Add to My InfoView �


In the past, I have taken the string and matched on a specific value so I know how to do that and I've used the indexOf method. But, how do I find a value based on static text surrounding it? I'm looking at String methods and regular expressions...I am trying to find out the best way to do this.

Thanks,
Laura
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Laura Wells:
...how do I find a value based on static text surrounding it? ...


Have you looked at StringTokenizer?
 
Laura Wells
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not yet. I haven't looked into StringTokenizer but I did get it to work using indexOf and substring methods. The code works but probably isn't optimized very well. I might look into StringTokenizer to see if that makes my code more streamlined.

Thanks for the suggestion!

Laura
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you know that the substring you want is always between substrings "Advanced..." and "Document(s)" AND you know that these substrings won't occur elsewhere prior to this, then using indexOf with substring is probably okay.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use a tokenizer, you are basically parsing the String into tokens (substrings) based on some delimiter (separator). So if you have a String like this:

"stuff here Advanced... 999 Document(s) more stuff here"

And you parse it with a delimiter of "Advanced... ", then you would get 2 tokens:
  • stuff here
  • 999 Document(s) more stuff here
  • Then if you parse the second of these with a delimiter of " Document(s) ", you would get 2 tokens:
  • 999
  • more stuff here
  • The first of these is what you want.

    But I don't see that this is any better than using indexOf.
    [ September 28, 2005: Message edited by: marc weber ]
     
    author
    Posts: 9050
    21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you do decide to experiment with StringTokenizer, you might want to consider the String.split method instead, as StringTokenizer has been deprecated.
     
    Ranch Hand
    Posts: 1608
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Bert Bates:
    If you do decide to experiment with StringTokenizer, you might want to consider the String.split method instead, as StringTokenizer has been deprecated.



    StringTokenizer has been obsoleted, not deprecated.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic