3

I have one String

String path = /PravinSB/servlet/com.gjgj.rmf.controllers.Hello

from this String I need only word - PravinSB.

How can I do this in JAVA

Shaharyar
  • 11,749
  • 4
  • 46
  • 62
Pravin Mohol
  • 605
  • 6
  • 16
  • possible duplicates of http://stackoverflow.com/questions/5414657/extract-substring-from-a-string – Ronak Dec 25 '13 at 09:31

4 Answers4

6

Try this

String path = /PravinSB/servlet/com.gjgj.rmf.controllers.Hello

String[] split = path.split("/");

String name = split[1]; //name is your result.
Sanket Shah
  • 4,448
  • 3
  • 19
  • 40
3
String path = /PravinSB/servlet/com.gjgj.rmf.controllers.Hello
String[] split = path.split("/");
String name = split[1]; //name is your result.

check below link

http://ideone.com/4Uzosq

Deepak
  • 1,729
  • 17
  • 28
0

Try this:

String path = /PravinSB/servlet/com.gjgj.rmf.controllers.Hello

String[] split = path.split("/");

String name = split[1];

also try this:

String path = /PravinSB/servlet/com.gjgj.rmf.controllers.Hello
String name=path.substring(1,status.length()-39);
Flexo
  • 84,884
  • 22
  • 182
  • 268
learner
  • 3,017
  • 2
  • 20
  • 31
0

If you are sure that PravinSB is going to be first item then all above answer is true. But in case even a single item is added say /a/PravinSB/servlet/com.gjgj.rmf.controllers.Hello then its going to be wrong solution.

String path = "/PravinSB/servlet/com.gjgj.rmf.controllers.Hello";
int indexOfString = path.indexOf("PravinSB");
int indexOfNextSlash = path.indexOf("/", indexOfString);
String name=path.substring(indexOfString,indexOfNextSlash);

this will work even if the string is

String path = "/a/PravinSB/servlet/com.gjgj.rmf.controllers.Hello";
Tabrej Khan
  • 894
  • 6
  • 15