简体   繁体   中英

How to get the string after last comma in java?

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 be cas


Note: There is a space between last comma and 'c' character which also needs to be removed. Also the pattern contains only one space after last comma.

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

Perhaps something like:

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

That is, I take the substring occurring after the last comma and remove surrounding white space afterwards...

Always think of looking for the answer for this sort of question in Apache Commons... the basic ones should probably be included with most builds. The most appropriate (not using regexes) is this:

StringUtils.substringAfterLast(String str, String separator)

But if you really want to use a regex for some reason there are a couple of methods which you might want to use, eg

String removeAll(String text, String regex)

or

String removeFirst(String text, String regex)

Amusingly, you can get what you want by doing this (relying on greedy quantifiers):

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

StringUtils is part of apache commons-lang3.

Apart from anything else there is no point re-inventing the wheel. But I can think of at least 2 other advantages:

  1. the Apache people can also be counted on to have developed well tested code dealing with many "gotchas".
  2. these Apache methods are usually well named: you don't have to clutter up your code with stoopid utility methods; instead you have a nice clean method which does what it says on the tin...

PS there's nothing to prevent you looking at the source code to check about what the method actually does. Usually they are very easy to understand.

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

Only one space:

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

0 to n spaces:

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

What about the following:

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

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

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

and one more :)

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

然后

str.trim()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM