Java: removing numeric values from string

前端 未结 7 1881
心在旅途
心在旅途 2020-12-01 18:03

I have suceeded with the help of this community in removing numeric values from user input, however, my code below will only retrieve the alpha characters before the numeric

7条回答
  •  天命终不由人
    2020-12-01 18:43

    /*Remove numbers from given specific string*/
    public class NewClass6 {
    
        public static void main(String[] args){
            String s= "hello647hi74joke";
            char[] ch= s.toCharArray();
            System.out.println("Result = " + getString(ch));
        }
    
        static String getString(char[] ch){
            int m = 0;
            char[] chr = new char[50];
            char[] k = {'0','1','2','3','4','5','6','7','8','9'};
    
            for(int i = 0; i < ch.length; i++){
               for(int j = 0; j < k.length; j++){  
                   if(ch[i]==k[j]){
                       m--;
                       break;
                   }
                   else {
                       chr[m]=ch[i];
                   }
                }
                m++;
             }
    
             String st = String.valueOf(chr);
             return st;
         }
    }
    

提交回复
热议问题