简体   繁体   English

如何从JAVA Android中的String获取某些特定单词?

[英]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. 从这个字符串我只需要一个字--PravinSB。

How can I do this in JAVA 我怎么能在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 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. 如果您确定PravinSB将成为第一项,那么以上所有答案都是正确的。 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. 但是如果连一个项目都加上说/a/PravinSB/servlet/com.gjgj.rmf.controllers.Hello那么它将是错误的解决方案。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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