cbll cbll - 15 days ago 6
Java Question

Java: Print specific substring based on characters appearing before, and end if meeting letters after?

I have an input string, let's say it's > pxs ikf len 3348
, it might also sometimes be input via the stream as > pxs ikf len 3348 BBBTTT

How do I capture the numbers occuring after "len" in the via substring? Using
system.out.println(stringStr.substring(stringStr.lastIndexOf(" ") + 1)
will only print
if it's the last part of the string above, but if BBBTTT appears, it will not work.

How do I print the numbers of a substring specifically after "len" but not including anything but the numbers that follow(might be 0-9999, not more), so not any characters following?


You can use split method for this.

        String s = " > pxs ikf len 3348 BBBTTT";
    s = s.split("len")[1];
    s = s.replaceAll("\\D+", "");

or just

s = s.split("len")[1].replaceAll("\\D+", "");