Removing whitespace from strings in Java

前端 未结 30 1889
一个人的身影
一个人的身影 2020-11-22 05:01

I have a string like this:

mysz = \"name=john age=13 year=2001\";

I want to remove the whitespaces in the string. I tried trim()

30条回答
  •  臣服心动
    2020-11-22 05:42

    You can also take a look at the below Java code. Following codes does not use any "built-in" methods.

    /**
     * Remove all characters from an alphanumeric string.
     */
    public class RemoveCharFromAlphanumerics {
    
        public static void main(String[] args) {
    
            String inp = "01239Debashish123Pattn456aik";
    
            char[] out = inp.toCharArray();
    
            int totint=0;
    
            for (int i = 0; i < out.length; i++) {
                System.out.println(out[i] + " : " + (int) out[i]);
                if ((int) out[i] >= 65 && (int) out[i] <= 122) {
                    out[i] = ' ';
                }
                else {
                    totint+=1;
                }
    
            }
    
            System.out.println(String.valueOf(out));
            System.out.println(String.valueOf("Length: "+ out.length));
    
            for (int c=0; c= 48 && (int) out[d] <=57 ) {
    
                    System.out.println(out[d]);
                    whitespace[t]= out[d];
                    t+=1;
    
                }
    
            }
    
            System.out.println("**********");
            System.out.println("**********");
    
            System.out.println("The String is: " + String.valueOf(whitespace));
    
        }
    }
    

    Input:

    String inp = "01239Debashish123Pattn456aik";
    

    Output:

    The String is: 01239123456
    

提交回复
热议问题