Print Only Numbers From Java Strings

I have a question in Java-string,
String s="adf5hgjyu1frdsff6vgyu7bnjh9asd3"; like
How to print only numbers in that string

Showing Answers 1 - 0 of 0 Answers

Amarendra

  • Oct 6th, 2015
 

Code
  1.  

  2. public static void main(String[] args) {

  3.                 String s = "adf5hgjyu1frdsff6vgyu7bnjh9asd3";

  4.                 char[] sarr = s.toCharArray();

  5.                 for (int i = 0; i < sarr.length; i++) {

  6.                         if(sarr[i]/1>=48 && sarr[i]/1<=57){

  7.                                 System.out.print(sarr[i]+", ");

  8.                         }else{

  9.                                 //System.out.print(sarr[i]+", ");

  10.                         }

  11.                 }

  12.         }

  Was this answer useful?  Yes

amrendra

  • Oct 6th, 2015
 

Check with ascii codes

Code
  1. public static void main(String[] args) {

  2.                 String s = "adf5hgjyu1frdsff6vgyu7bnjh9asd3";

  3.                 char[] sarr = s.toCharArray();

  4.                 for (int i = 0; i < sarr.length; i++) {

  5.                         if(sarr[i]/1>=48 && sarr[i]/1<=57){

  6.                                 System.out.print(sarr[i]+", ");

  7.                         }else{

  8.                                 //System.out.print(sarr[i]+", ");

  9.                         }

  10.                 }

  11.         }

  Was this answer useful?  Yes

rawatfeb

  • Feb 23rd, 2016
 

private static void NumericsFromStringTest() {
String alphaNumericString = "h8ll0";
char[] carray = alphaNumericString.toCharArray();
for (char c : carray) {
if (Character.isDigit(c)) {
System.out.println(c);
}
}
}

Code
  1.         private static void NumericsFromStringTest() {

  2.                 String alphaNumericString = "h8ll0";

  3.                 char[] carray = alphaNumericString.toCharArray();

  4.  

  5.                 for (char c : carray) {

  6.  

  7.                         if (Character.isDigit(c)) {

  8.  

  9.                                 System.out.println(c);

  10.                         }

  11.  

  12.                 }

  13.  

  14.         }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions