Removing whitespace from strings in Java

前端 未结 30 1846
一个人的身影
一个人的身影 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:41

    st.replaceAll("\\s+","") removes all whitespaces and non-visible characters (e.g., tab, \n).


    st.replaceAll("\\s+","") and st.replaceAll("\\s","") produce the same result.

    The second regex is 20% faster than the first one, but as the number consecutive spaces increases, the first one performs better than the second one.


    Assign the value to a variable, if not used directly:

    st = st.replaceAll("\\s+","")
    
    0 讨论(0)
  • 2020-11-22 05:42

    How about replaceAll("\\s", ""). Refer here.

    0 讨论(0)
  • 2020-11-22 05:42

    If you need to remove unbreakable spaces too, you can upgrade your code like this :

    st.replaceAll("[\\s|\\u00A0]+", "");
    
    0 讨论(0)
  • 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<out.length; c++){
    
                System.out.println(out[c] + " : " + (int) out[c]);
    
                if ( (int) out[c] == 32) {
                    System.out.println("Its Blank");
                     out[c] = '\'';
                }
    
            }
    
            System.out.println(String.valueOf(out));
    
            System.out.println("**********");
            System.out.println("**********");
            char[] whitespace = new char[totint];
            int t=0;
            for (int d=0; d< out.length; d++) {
    
                int fst =32;
    
    
    
                if ((int) out[d] >= 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
    
    0 讨论(0)
  • 2020-11-22 05:43

    there are many ways to solve this problem. you can use split function or replace function of Strings.

    for more info refer smilliar problem http://techno-terminal.blogspot.in/2015/10/how-to-remove-spaces-from-given-string.html

    0 讨论(0)
  • 2020-11-22 05:44

    You've already got the correct answer from Gursel Koca but I believe that there's a good chance that this is not what you really want to do. How about parsing the key-values instead?

    import java.util.Enumeration;
    import java.util.Hashtable;
    
    class SplitIt {
      public static void main(String args[])  {
    
        String person = "name=john age=13 year=2001";
    
        for (String p : person.split("\\s")) {
          String[] keyValue = p.split("=");
          System.out.println(keyValue[0] + " = " + keyValue[1]);
        }
      }
    }
    

    output:
    name = john
    age = 13
    year = 2001

    0 讨论(0)
提交回复
热议问题