How to get the string after last comma in java?

后端 未结 9 749
谎友^
谎友^ 2020-12-18 18:14

How do I get the content after the last comma in a string using a regular expression?

Example:

abcd,fg;ijkl, cas

The output should

9条回答
  •  春和景丽
    2020-12-18 18:36

    Using regular expressions:

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

    Outputs:

    cas
    

    Or you can use simple String methods:

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

提交回复
热议问题