aspose file tools*
The moose likes Beginning Java and the fly likes Help with getting a specific value from a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with getting a specific value from a String" Watch "Help with getting a specific value from a String" New topic
Author

Help with getting a specific value from a String

Eric Martin
Greenhorn

Joined: Jul 15, 2005
Posts: 22
I have a String that I need to break into parts to use one of the values.

Example:
// This value actually comes from request.getReqestURI();
String uri = "/Servlet/Case.html";

What I need is the "Case" value...it won't always be "Case". The format will always be ContextPath + / + pageName and what I need is the pageName without the .html.

TIA
David C. Meyers
Greenhorn

Joined: May 20, 2004
Posts: 14
May I suggest:

String uri = "/Servlet/Case.html";
int lastSlashIndex = url.lastIndexOf('/');
int lastDotIndex = url.lastIndexOf('.');
String name = uri.substring(lastSlashIndex, lastDotIndex);

Good luck.
Eric Martin
Greenhorn

Joined: Jul 15, 2005
Posts: 22
Much more elegant than what I had =)

I just had to change:
String name = uri.substring(lastSlashIndex, lastDotIndex);
to:
String name = uri.substring(lastSlashIndex + 1, lastDotIndex);

Thanks a bunch!
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
If you're programming with servlets, you can always ask the request what the context path is rather than parsing it out of the URI.


James Carman, President<br />Carman Consulting, Inc.
Eric Martin
Greenhorn

Joined: Jul 15, 2005
Posts: 22
Right, but in my example, that would just give me /Servlet and I wanted Case. Correct me if I am wrong.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

THIRD AND FINAL NOTICE

You had it correct before, why did you change it back?

"Eric"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580


When I access the page it says...


The context path is /jsp-examples!

[ August 06, 2005: Message edited by: James Carman ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with getting a specific value from a String