Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting a substring

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a brain fart.
I am reading in a text file line by line. In each line I need to get a string that exists between 2 different strings. For example, If I had a string
{A Start=Now is the time/A}
I need to return the string
Now is the time.
Can someone give me a shove in the right direction? Also, The string I need to get will be different every time, but the start and ending elements will always be the same.
Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if the start and end tags are always the same, then you could just do

Don't think you can do much better than that.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could find the index for the = and the / and get everything in between. Simple enough to do using indexOf and lastIndexOf, as long as the first '=' is always right before the string and the last / is always right after it, which appears to be the case.

EDIT: typo ;p
[ August 08, 2003: Message edited by: Ken Blair ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works great guys! Thanks a bunch.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also do this with regular expressions:

The other techniques are probably a little faster, but regex can be used far more flexibly, if your requirements change...
 
VIJAY Yadlapati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like XML tags, why don't you go for XML?
Make use of Technology - take advantage of it
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by VIJAY Yadlapati:
It seems like XML tags, why don't you go for XML?
Make use of Technology - take advantage of it

It's not XML tags and I have no control over the input. Thanks anyway.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic