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

Help with getting a specific value from a String

 
Eric Martin
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eric Martin
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When I access the page it says...


The context path is /jsp-examples!

[ August 06, 2005: Message edited by: James Carman ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic