Solved

How to get a substring from a URI

Posted on 2006-11-22
7
879 Views
Last Modified: 2010-05-18
I need to get a name from differnt URI's

The URI can be in theis formats

http://localhost:8080/chapter_admin/steve

or

http://localhost:8080/chapter_admin/steve?action=dsdsd&L=dasdas&P=dadsdasd&uid=ddasda

I am using this code

String chapter = request.getRequestURI().substring(15);

this code works fine with the first URL but not with the second, I only need "steve" from both these url's , would anyone know how to do this

Thanks
0
Comment
Question by:huzefaq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17997338
0
 

Author Comment

by:huzefaq
ID: 17997436
no ths actually gets
/chapter_admin/alabama/
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 17999206
String[] x = request.getServletPath().split("/");
String name = x[x.length-1];
0
 
LVL 3

Assisted Solution

by:nishayr
nishayr earned 100 total points
ID: 18000474
Hey there,

You could also try

URL url = new URL(request.getRequestURI());

then you have access to all the individual components of the url using:

url.getHost(), url.getPort, url.getProtocol() etc....

Thanks,
Nish
0
 
LVL 2

Assisted Solution

by:glottis
glottis earned 100 total points
ID: 18001232
String chapter = request.getRequestURI();
int index = chapter.lastIndexOf("/");
chapter = chapter.substring(index);
index = chapter.indexOf("?");
if (index > 0)
  chapter = chapter.substring(0, index);
System.out.println(chapter);

Try that, maybe that would help you out.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question