• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with getting a specific value from a String

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
it's a teeny, tiny, wafer thin ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic