【问题标题】:How to get the string after last comma in java?如何在java中获取最后一个逗号后的字符串?
【发布时间】:2012-03-19 21:44:43
【问题描述】:

如何使用正则表达式获取字符串中最后一个逗号后的内容?

例子:

abcd,fg;ijkl, cas

输出应该是cas


注意:最后一个逗号和'c' 字符之间有一个空格,也需要删除。 此外,该模式在最后一个逗号后仅包含一个空格。

【问题讨论】:

    标签: java regex


    【解决方案1】:

    使用正则表达式:

    Pattern p = Pattern.compile(".*,\\s*(.*)");
    Matcher m = p.matcher("abcd,fg;ijkl, cas");
    
    if (m.find())
        System.out.println(m.group(1));
    

    输出:

    cas
    

    或者你可以使用简单的String 方法:

    1. System.out.println(s.substring(s.lastIndexOf(",") + 1).trim());
    2. System.out.println(s.substring(s.lastIndexOf(", ") + 2));

    【讨论】:

    • 添加正则表达式示例!
    • @dacwe : 和最后一个索引一样,如何获取第一个索引??
    • @Deepthi: String.indexOf 这样做(但这确实是另一个问题)。
    【解决方案2】:

    可能是这样的:

    String s = "abcd,fg;ijkl, cas";
    String result = s.substring(s.lastIndexOf(',') + 1).trim();
    

    也就是说,我取最后一个逗号之后出现的子字符串,然后删除周围的空格......

    【讨论】:

    • 注意你的情况!子字符串而不是子字符串
    • String s = "C:\\Users\\dataset\\Files\\Upload\\samplejpg.jpg";字符串结果 = s.substring(s.lastIndexOf('\\') + 1).trim();我用它从路径中提取带有扩展名的文件名..
    【解决方案3】:

    你可以试试这个:

    public static void main(String[] args) {
        String s = " abcd,fg;ijkl, cas";
        String[] words = s.split(",");
        System.out.println(words[words.length-1].trim());
    }
    

    【讨论】:

    • 对这个简单的搜索使用split 并不是最佳选择。你可以使用lastIndexOf
    • @MohammadDehghan 不是最优的,它是多少皮秒? :-)
    【解决方案4】:

    只有一个空格:

    String[] aux = theInputString.split(",\\s");
    string result = aux[aux.length-1];
    

    0 到 n 个空格:

    String[] aux = theInputString.split(",\\s*");
    string result = aux[aux.length-1];
    

    【讨论】:

      【解决方案5】:
      str.split(",");
      

      然后

      str.trim()
      

      【讨论】:

        【解决方案6】:

        下面的呢:

        String a = "abcd,fg;ijkl, cas";
        String[] result = a.split(",[ ]*");
        String expectedResult = result[result.length - 1]);
        

        【讨论】:

        【解决方案7】:

        还有一个:)

        String s = "abcd,fg;ijkl, cas";
        String result = s.replaceAll(".*, ", "");
        

        【讨论】:

          【解决方案8】:

          始终考虑在 Apache Commons 中寻找此类问题的答案……基本的问题可能应该包含在大多数构建中。最合适的(不使用正则表达式)是这样的:

          StringUtils.substringAfterLast(String str, String separator)
          

          但是,如果您出于某种原因真的想使用正则表达式,则可能需要使用几种方法,例如

          String removeAll(String text, String regex)
          

          String removeFirst(String text, String regex)
          

          有趣的是,你可以通过这样做得到你想要的(依靠贪婪的量词):

          StringUtils.removeFirst( myString, ".*," );
          

          StringUtils 是 apache commons-lang3 的一部分。

          除此之外,重新发明轮子毫无意义。但我能想到至少 2 个其他优点:

          1. Apache 人员也可以指望他们开发了经过良好测试的代码来处理许多“陷阱”。
          2. 这些 Apache 方法的名称通常都很好:您不必使用 stoopid 实用程序方法弄乱您的代码;取而代之的是,您有一个很好的干净方法,可以按照锡上所说的那样做...

          PS 没有什么可以阻止您查看源代码以检查该方法的实际作用。通常它们很容易理解。

          【讨论】:

            【解决方案9】:

            我用答案解决了我的问题,只需要从路径中提取文件名,

            String s = "C:\\\\Users\\\\Blue Tag\\\\Files\\\\Upload\\\\samplejpg.jpg"; String result = s.substring(s.lastIndexOf('\\') + 1).trim();

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-11-11
              • 2019-10-25
              • 1970-01-01
              • 2020-10-09
              • 2013-11-25
              • 2018-04-29
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多