Java String remove white spaces from right
//package com.demo2s; public class Main { public static void main(String[] argv) throws Exception { String str = " demo2s.com "; System.out.println(">"+rstrip(str)+"<"); }//from w w w. j av a2 s . c o m public static final String WHITE_SPACES = " \r\n\t\u3000\u00A0\u2007\u202F"; /** rstrip - strips spaces from right * @param str what to strip * @return String the striped string */ public static String rstrip(String str) { return megastrip(str, false, true, WHITE_SPACES); } /** * This is a both way strip * * @param str the string to strip * @param left strip from left * @param right strip from right * @param what character(s) to strip * @return the stripped string */ public static String megastrip(String str, boolean left, boolean right, String what) { if (str == null) { return null; } int limitLeft = 0; int limitRight = str.length() - 1; while (left && limitLeft <= limitRight && what.indexOf(str.charAt(limitLeft)) >= 0) { limitLeft++; } while (right && limitRight >= limitLeft && what.indexOf(str.charAt(limitRight)) >= 0) { limitRight--; } return str.substring(limitLeft, limitRight + 1); } }