Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

java regex to find unique digits

I have a string of some length (may vary) with digits, for example:

String strInput = "0002334556";

I need to get all unique digits from strInput and allocate them to a different string like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

String uniques = "023456";

Currently stuck with what regex I should use to parse through strInput. Non-regex solutions are also welcome. This is not a real life task, but an academic one which favors regex.

>Solution :

Try this.

public static void main(String[] args) {
    String strInput = "00023345560303";

    String uniques = Arrays.stream(strInput.split("(?<=.)"))
        .distinct().collect(Collectors.joining());

    System.out.println(uniques);
}

output:

023456
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading