简体   繁体   中英

I want to filter out the last Number in String

My String is

"Success Entries and Failed Entries: {FAILED_ENTRIES={}, SUCCESS_ENTRIES={123=1509170142065114105}}"

Here i want to filter out only 1509170142065114105 this string here 123 can be anything.

how can I do it ?

You can use regex .*(?:\\\\D|^)(\\\\d+).* to get last number in the String .

USE:

public static void main(String[] args) {
    String s = "{FAILED_ENTRIES={}, SUCCESS_ENTRIES={123=1509170142065114105}}";
    System.out.println(s.replaceAll(".*(?:\\D|^)(\\d+).*", "$1"));
}

OUTPUT:

1509170142065114105

DEMO:

Check here a working demo .

A regex will work for you.

public static void main(String args[]) {
    String s = "{FAILED_ENTRIES={}, SUCCESS_ENTRIES={123=1509170142065114105}}";
    System.out.println(s.replaceAll(".*=(\\d+).*", "$1"));
}

O/P :

1509170142065114105

If all your strings are of same format then you can use lastIndexOf and substring .

    String s = "{FAILED_ENTRIES={}, SUCCESS_ENTRIES={123=1509170142065114105}}";
    int lastEqualIndex = s.lastIndexOf("=");

    String lastNumber = s.substring(lastEqualIndex + 1, s.length() - 2);
    System.out.println(lastNumber);

Output 1509170142065114105

If your sting formats can vary then the regex solutions posted by @Jordi Castilla should be better.

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