File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Getting a substring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting a substring" Watch "Getting a substring" New topic
Author

Getting a substring

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

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.


GenRocket - A Test Data Generation Platform
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Works great guys! Thanks a bunch.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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...


"I'm not back." - Bill Harding, Twister
VIJAY Yadlapati
Ranch Hand

Joined: Aug 04, 2003
Posts: 175
It seems like XML tags, why don't you go for XML?
Make use of Technology - take advantage of it
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting a substring
 
Similar Threads
Is there an easy way to get a String from jTextArea?
how does the compiler work with identifiers ??
Addind a word in the String
Need advice on improving performance
Garbage Collection!