Question d’entretien chez Apollo Education Group

write a program that given a varargs string argument, appends only the number part to the resultant string buffer.

Réponse à la question d'entretien

Utilisateur anonyme

29 juin 2012

package codesamples; import java.util.Scanner; public class SplitString { public SplitString() { // TODO Auto-generated constructor stub } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.print("Please enter a string: "); String s = input.nextLine(); System.out.println(); System.out.println("The string you enter is: " + s); //get the new string String charString = s.replaceAll("\\d+", "---"); System.out.println("-----------> " + charString); //now get the non-digit part String numberString = s.replaceAll("\\D", " "); System.out.println("-----------> " + numberString); //Now append to the string that was String fullString = new String(charString + numberString); System.out.println("-----------> " + fullString); /********************** * requirements are not clear - so this is how I understood it.... * Please enter a string: a122b344 this is 555 a test The string you enter is: a122b344 this is 555 a test -----------> a---b--- this is --- a test -----------> 122 344 555 -----------> a---b--- this is --- a test 122 344 555 ****************/ } }