Java String remove from both side
//package com.demo2s; public class Main { public static void main(String[] argv) throws Exception { String str = "mdemo2s.com"; boolean left = true; boolean right = true; String what = "m"; System.out.println(megastrip(str, left, right, what)); }//from www. ja va 2 s.c om /** * 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); } }