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
  • 12,254
  • 4
  • 46
  • 66
Pravin Mohol
  • 665
  • 6
  • 17

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,352
  • 3
  • 21
  • 41
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
  • 2,287
  • 1
  • 23
  • 30
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
  • 87,323
  • 22
  • 191
  • 272
learner
  • 3,092
  • 2
  • 21
  • 33
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