How to remove trailing spaces of a String in Java

1 Answer

0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            String s = "    java   ";
            
            s = s.replaceFirst("\\s+$", "");
            System.out.println(":" + s + ":");

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:

:    java:
    
 */

 



answered Nov 27, 2016 by avibootz

Related questions

1 answer 158 views
1 answer 203 views
1 answer 167 views
1 answer 135 views
1 answer 135 views
1 answer 223 views
...