• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parsing and getting a value from a String  RSS feed

 
Laura Wells
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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 ]
     
    Bert Bates
    author
    Sheriff
    Posts: 8945
    17
    • Mark post as helpful
    • send pies
    • 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.
     
    Tony Morris
    Ranch Hand
    Posts: 1608
    • Mark post as helpful
    • send pies
    • 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.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!