如何在JAVA Android中从字符串中获取特定单词?

3

我有一个字符串

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

我需要从这个字符串中只提取单词 - PravinSB。

我该如何在JAVA中实现这个功能。


可能重复的内容:https://dev59.com/VG035IYBdhLWcg3wcvqS - Ronak Jain
4个回答

6

试试这个

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

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

String name = split[1]; //name is your result.

split[1] 将包含结果。 - Deepak

3
String path = /PravinSB/servlet/com.gjgj.rmf.controllers.Hello
String[] split = path.split("/");
String name = split[1]; //name is your result.

请查看下方链接

http://ideone.com/4Uzosq


0

试试这个:

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

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

String name = split[1];

也可以试试这个:

String path = /PravinSB/servlet/com.gjgj.rmf.controllers.Hello
String name=path.substring(1,status.length()-39);

0
如果您确定PravinSB将成为第一个项目,那么以上所有答案都是正确的。但是,如果添加了单个项目,例如/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);

即使字符串是这样,这也可以工作

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接