remove Leading And Trailing Spaces with regex - Java java.lang

Java examples for java.lang:String Strip

Description

remove Leading And Trailing Spaces with regex

Demo Code


//package com.java2s;

public class Main {
    public static String removeLeadingAndTrailingSpaces(String in) {
        return removeLeadingSpaces(removeTrailingSpaces(in));
    }//w w  w  . ja  v a2s  .c  o  m

    public static String removeLeadingSpaces(String in) {
        return in.replaceAll("^[\\s]+", "");
    }

    public static String removeTrailingSpaces(String in) {
        return in.replaceAll("[\\s]+$", "");
    }
}

Related Tutorials