Popular Posts

이은한. Powered by Blogger.

2022년 3월 16일 수요일

Delete all spaces in String at JAVA


By String method

yourString.replaceAll(" ", "");

By regular expression

yourString.replaceAll("\\p{Z}", "");

Explanation

Most situations, using the string method will be good enough to delete all spaces in String. 


However, computers have some charsets that represent spaces such as IDEOGRAPHIC SPACE. 


In this case, use the regular expression to delete all the spaces.


Example

        String yourString = " this is\u3000test ru n";
        System.out.println(yourString);

        String byMethod = yourString.replaceAll(" ", "");
        System.out.println("byMethod: "+byMethod);

        String byRegularExpression = yourString.replaceAll("\\p{Z}", "");
        System.out.println("byRegularExpression: "+byRegularExpression);

Result

 this is test ru n // original String
thisis testrun // by method
thisistestrun // by regular expression

0 개의 댓글:

댓글 쓰기