简体   繁体   中英

how to get some particular word from String in JAVA Android?

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

Try this

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

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

String name = split[1]; //name is your result.
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

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);

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM