Search code examples
javasplittokenquotes

"5":"6789":"78" , how can we extract just the numbers as tokens


For example we have our data set like this "5":"6789":"78" , how can we extract just the numbers as tokens, we know how to split using colon, but there is problem to remove these quotes, after it is been transferred into the array, please suggest.


Solution

  • Just split based on one or more non-numeric characters.

    public static void main(String[] args) {
    
        String s ="5\":\"6789\":\"78";
        String[] arr = s.split("\\D+");// \\D+ splits the string based on one or more non-numeric characters.
        for(String str :arr){
            System.out.println(str);
        }
    }
    

    O/P:

    5
    6789
    78