How to get the string after last comma in java?

后端 未结 9 700
谎友^
谎友^ 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:42

    I used the answers to solve my problem, it just need to extract the file name from path,

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

    0 讨论(0)
  • 2020-12-18 18:44

    You could try this:

    public static void main(String[] args) {
        String s = " abcd,fg;ijkl, cas";
        String[] words = s.split(",");
        System.out.println(words[words.length-1].trim());
    }
    
    0 讨论(0)
  • and one more :)

    String s = "abcd,fg;ijkl, cas";
    String result = s.replaceAll(".*, ", "");
    
    0 讨论(0)
提交回复
热议问题