Java String remove substring from start and end
//package com.demo2s; public class Main { public static void main(String[] argv) throws Exception { String str = ".comdemo2s.com"; System.out.println(strip(str,".com")); }/* w w w . ja v a 2 s . c om*/ /** * The empty String <code>""</code>. * @since 2.0 */ public static final String EMPTY = ""; /** * <p>Strips any of a set of characters from the start and end of a String. * This is similar to {@link String#trim()} but allows the characters * to be stripped to be controlled.</p> * * <p>A <code>null</code> input String returns <code>null</code>. * An empty string ("") input returns the empty string.</p> * * <p>If the stripChars String is <code>null</code>, whitespace is * stripped as defined by {@link Character#isWhitespace(char)}. * Alternatively use {@link #strip(String)}.</p> * * <pre> * strip(null, *) = null * strip("", *) = "" * strip("abc", null) = "abc" * strip(" abc", null) = "abc" * strip("abc ", null) = "abc" * strip(" abc ", null) = "abc" * strip(" abcyx", "xyz") = " abc" * </pre> * * @param str the String to remove characters from, may be null * @param stripChars the characters to remove, null treated as whitespace * @return the stripped String, <code>null</code> if null String input */ public static String strip(String str, String stripChars) { if (isEmpty(str)) { return str; } str = stripStart(str, stripChars); return stripEnd(str, stripChars); } /** * <p>Checks if a String is empty ("") or null.</p> * * <pre> * isEmpty(null) = true * isEmpty("") = true * isEmpty(" ") = false * isEmpty("bob") = false * isEmpty(" bob ") = false * </pre> * * <p>NOTE: This method changed in Lang version 2.0. * It no longer trims the String. * That functionality is available in isBlank().</p> * * @param str the String to check, may be null * @return <code>true</code> if the String is empty or null */ public static boolean isEmpty(String str) { return str == null || str.length() == 0; } /** * <p>Strips any of a set of characters from the start of a String.</p> * * <p>A <code>null</code> input String returns <code>null</code>. * An empty string ("") input returns the empty string.</p> * * <p>If the stripChars String is <code>null</code>, whitespace is * stripped as defined by {@link Character#isWhitespace(char)}.</p> * * <pre> * stripStart(null, *) = null * stripStart("", *) = "" * stripStart("abc", "") = "abc" * stripStart("abc", null) = "abc" * stripStart(" abc", null) = "abc" * stripStart("abc ", null) = "abc " * stripStart(" abc ", null) = "abc " * stripStart("yxabc ", "xyz") = "abc " * </pre> * * @param str the String to remove characters from, may be null * @param stripChars the characters to remove, null treated as whitespace * @return the stripped String, <code>null</code> if null String input */ public static String stripStart(String str, String stripChars) { int strLen; if (str == null || (strLen = str.length()) == 0) { return str; } int start = 0; if (stripChars == null) { while ((start != strLen) && Character.isWhitespace(str.charAt(start))) { start++; } } else if (stripChars.length() == 0) { return str; } else { while ((start != strLen) && (stripChars.indexOf(str.charAt(start)) != -1)) { start++; } } return str.substring(start); } /** * <p>Strips any of a set of characters from the end of a String.</p> * * <p>A <code>null</code> input String returns <code>null</code>. * An empty string ("") input returns the empty string.</p> * * <p>If the stripChars String is <code>null</code>, whitespace is * stripped as defined by {@link Character#isWhitespace(char)}.</p> * * <pre> * stripEnd(null, *) = null * stripEnd("", *) = "" * stripEnd("abc", "") = "abc" * stripEnd("abc", null) = "abc" * stripEnd(" abc", null) = " abc" * stripEnd("abc ", null) = "abc" * stripEnd(" abc ", null) = " abc" * stripEnd(" abcyx", "xyz") = " abc" * </pre> * * @param str the String to remove characters from, may be null * @param stripChars the characters to remove, null treated as whitespace * @return the stripped String, <code>null</code> if null String input */ public static String stripEnd(String str, String stripChars) { int end; if (str == null || (end = str.length()) == 0) { return str; } if (stripChars == null) { while ((end != 0) && Character.isWhitespace(str.charAt(end - 1))) { end--; } } else if (stripChars.length() == 0) { return str; } else { while ((end != 0) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) { end--; } } return str.substring(0, end); } }