This question is asked in my interview with one the top mnc.
Question : Replace all digits in string with '*' in Java
Solution :
public class Test { public static void main(String[] args) { System.out.println(replaceInts("I will eat 2 burgers 2345 fries & 21.25 cokes l8r")); } public static String replaceInts(String strTemp) { String str=strTemp; for(int i=0; i<str.length(); i++) { if(Character.isDigit(str.charAt(i))) { str = str.replaceFirst("[0-9]+", "*"); } } return str; } }
0 comments:
Post a Comment