java trim start end space

Java program that trims starts and ends

public class Program {

    public static String trimEnd(String value) {
        // Use replaceFirst to remove trailing spaces.
        return value.replaceFirst("\\s+$", "");
    }

    public static String trimStart(String value) {
        // Remove leading spaces.
        return value.replaceFirst("^\\s+", "");
    }

    public static void main(String[] args) {

        String value = "  /Test?  ";
        // Use custom trimEnd and trimStart methods.
        System.out.println("[" + trimEnd(value) + "]");
        System.out.println("[" + trimStart(value) + "]");
    }
}
posted @ 2018-04-25 18:58  kakaisgood  阅读(373)  评论(0编辑  收藏  举报