【问题标题】:how to get some particular word from String in JAVA Android?如何从 JAVA Android 中的 String 中获取一些特定的单词?
【发布时间】:2013-12-25 09:30:03
【问题描述】:

我有一个字符串

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

从这个字符串中我只需要一个词 - PravinSB。

如何在 JAVA 中做到这一点

【问题讨论】:

标签: java android string


【解决方案1】:

试试这个

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

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

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

【讨论】:

  • split[1] 将包含结果
【解决方案2】:
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

【讨论】:

    【解决方案3】:

    试试这个:

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

    【讨论】:

      【解决方案4】:

      如果您确定 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";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-08
        • 2019-05-17
        • 2013-06-19
        • 1970-01-01
        • 1970-01-01
        • 2021-06-28
        • 1970-01-01
        • 2014-02-22
        相关资源
        最近更新 更多