6 Replies - 2786 Views - Last Post: 04 August 2011 - 02:09 PM Rate Topic: -----

#1 Handler  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 199
  • Joined: 01-April 10

Best way to extract part of string

Posted 04 August 2011 - 10:02 AM

Now i have this string
 <string>1210.00</string>


What would be the fastest way to extract what ever is between the <string></string>?

I have a few methods in mind but since you all rock so much I am betting that you have faster ways :P

EDIT: by fastest I mean shortest way to type

This post has been edited by Handler: 04 August 2011 - 10:03 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Best way to extract part of string

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Re: Best way to extract part of string

Posted 04 August 2011 - 10:29 AM

Take a look at the Java XML Parsing libraries. KYA has blog entries on SAX, DOM, and StAX for parsing XML. Read up on them and pick your favorite. :)

Link: http://www.dreaminco...op/page__st__20
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,748
  • Joined: 19-March 11

Re: Best way to extract part of string

Posted 04 August 2011 - 10:30 AM

Fastest in run-time? Probably doesn't matter at all. Developer time is more likely to be the scarce resource here, so, since I'd have to look up the regex syntax to split on left or right angle bracket, I'd do
s.substring(s.indexOf(">"), s.indexOf("<"));


And then check the output to get the offsets right. About two minutes to get it running that way, I'd say.

EDIT: mac is a serious nerd, so for him, XML libraries are already loaded in his head. For me, not so much, so I'll do this one manually. :)
If your priorities put "robust" above "quick dev time", then existing XML libraries are indeed the way to do this. For run-time speed, I suspect regex might have the advantage on this limited application, but I don't have any real basis for that.

This post has been edited by jon.kiparsky: 04 August 2011 - 10:35 AM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Best way to extract part of string

Posted 04 August 2011 - 01:22 PM

View Postjon.kiparsky, on 04 August 2011 - 01:30 PM, said:

s.substring(s.indexOf(">"), s.indexOf("<"));

I am afraid the second argument will always be 0 trapping the first character of the String which is < :)
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Re: Best way to extract part of string

Posted 04 August 2011 - 01:23 PM

Quote

EDIT: mac is a serious nerd, so for him, XML libraries are already loaded in his head. For me, not so much, so I'll do this one manually. :)

I figured where there is one XML tag, there are more. If this is the only XML tag, I would agree with you 100%. :)
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Best way to extract part of string

Posted 04 August 2011 - 01:24 PM

may be s.indexOf("</")
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,748
  • Joined: 19-March 11

Re: Best way to extract part of string

Posted 04 August 2011 - 02:09 PM

View Postpbl, on 04 August 2011 - 03:22 PM, said:

Quote

s.substring(s.indexOf(">"), s.indexOf("<"));

I am afraid the second argument will always be 0 trapping the first character of the String which is < :)


Too true. Your "</" suggestion would do the work, or you could do
substring(s.indexOf(">").substring(indexOf("<"));


I think that might work.

(can we add an embedded java compiler to the dic editor? that'd be swell, thanks!)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1