Write a java method onlydigits that takes a string as a parameter. the method should remove from the string all characters, which are not digits, and return a string that contains only digits.

Answers

Answer is(a) the word icon in the taskbar is selected good luck: )

save and proof (apex verified)

public static String onlyDigits(String in){

    String digitsOnly = in.replaceAll("[^0-9]", "");

    return digitsOnly;

}

A Complete program is wrtten in the explanation section

Explanation:

public class TestClass {

   public static void main(String[] args) {

     String a = "-jaskdh2367sd.27askjdfh23";

       System.out.println(onlyDigits(a));

}

public static String onlyDigits(String in){

    String digitsOnly = in.replaceAll("[^0-9]", "");

    return digitsOnly;

}

}

The output: 23672723

The main logic here is using the Java replaceAll method which returns a string after replacing all the sequence of characters that match its argument, regex with an empty string

public static String onlyDigits(String in){

    String digitsOnly = in.replaceAll("[^0-9]", "");

    return digitsOnly;

}

A Complete program is wrtten in the explanation section

Explanation:

public class TestClass {

   public static void main(String[] args) {

     String a = "-jaskdh2367sd.27askjdfh23";

       System.out.println(onlyDigits(a));

}

public static String onlyDigits(String in){

    String digitsOnly = in.replaceAll("[^0-9]", "");

    return digitsOnly;

}

}

The output: 23672723

The main logic here is using the Java replaceAll method which returns a string after replacing all the sequence of characters that match its argument, regex with an empty string



Do you know the answer?

Other questions on the subject: Computers and Technology

The answer is C. Edits made to embedded data don't change the data in the source file, nor will edits made to the source file be reflected in the embedded data.We have to understan...Read More
1 more answers
You have several options when you want to enter data manually in excel. you can enter data in one cell, in several cells at the same time, or on more than one worksheet at the same...Read More
2 more answers
the correct sequence is 10.-The abolition of slavery and the right of women to vote  11.-The evil nature of men towards each other  12.-The expansion of educational opportunities...Read More
1 more answers