How to extract numbers from a string and get an array of ints?

后端 未结 13 1122
孤城傲影
孤城傲影 2020-11-22 05:32

I have a String variable (basically an English sentence with an unspecified number of numbers) and I\'d like to extract all the numbers into an array of integers. I was wond

13条回答
  •  感动是毒
    2020-11-22 05:54

    I would suggest to check the ASCII values to extract numbers from a String Suppose you have an input String as myname12345 and if you want to just extract the numbers 12345 you can do so by first converting the String to Character Array then use the following pseudocode

        for(int i=0; i < CharacterArray.length; i++)
        {
            if( a[i] >=48 && a[i] <= 58)
                System.out.print(a[i]);
        }
    

    once the numbers are extracted append them to an array

    Hope this helps

提交回复
热议问题