Java examples for java.lang:String Strip
Strips whitespace from the start and end of a String returning an empty String if null input. This is similar to #trimToEmpty(String) but removes whitespace.
/*//from w w w . ja v a 2 s .c o m * Copyright 2013 Guidewire Software, Inc. */ /** * This class is based, in part, on org.apache.commons.lang.StringUtils and is intended * to break the dependency on that project. * * @author <a href="http://jakarta.apache.org/turbine/">Apache Jakarta Turbine</a> * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> * @author Daniel L. Rall * @author <a href="mailto:gcoladonato@yahoo.com">Greg Coladonato</a> * @author <a href="mailto:ed@apache.org">Ed Korthof</a> * @author <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a> * @author Stephen Colebourne * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a> * @author Holger Krauth * @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a> * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> * @author Arun Mammen Thomas * @author Gary Gregory * @author Phil Steitz * @author Al Chou * @author Michael Davey * @author Reuben Sivan * @author Chris Hyzer * Johnson */ //package com.java2s; public class Main { public static void main(String[] argv) { String str = "java2s.com"; System.out.println(stripToEmpty(str)); } /** * The empty String <code>""</code>. * @since 2.0 */ public static final String EMPTY = ""; /** * <p>Strips whitespace from the start and end of a String returning * an empty String if <code>null</code> input.</p> * * <p>This is similar to {@link #trimToEmpty(String)} but removes whitespace. * Whitespace is defined by {@link Character#isWhitespace(char)}.</p> * * <pre> * stripToEmpty(null) = "" * stripToEmpty("") = "" * stripToEmpty(" ") = "" * stripToEmpty("abc") = "abc" * stripToEmpty(" abc") = "abc" * stripToEmpty("abc ") = "abc" * stripToEmpty(" abc ") = "abc" * stripToEmpty(" ab c ") = "ab c" * </pre> * * @param str the String to be stripped, may be null * @return the trimmed String, or an empty String if <code>null</code> input * @since 2.0 */ public static String stripToEmpty(String str) { return str == null ? EMPTY : strip(str, null); } /** * <p>Strips whitespace from the start and end of a String.</p> * * <p>This is similar to {@link #trim(String)} but removes whitespace. * Whitespace is defined by {@link Character#isWhitespace(char)}.</p> * * <p>A <code>null</code> input String returns <code>null</code>.</p> * * <pre> * strip(null) = null * strip("") = "" * strip(" ") = "" * strip("abc") = "abc" * strip(" abc") = "abc" * strip("abc ") = "abc" * strip(" abc ") = "abc" * strip(" ab c ") = "ab c" * </pre> * * @param str the String to remove whitespace from, may be null * @return the stripped String, <code>null</code> if null String input */ public static String strip(String str) { return strip(str, null); } /** * <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> * StringUtils.isEmpty(null) = true * StringUtils.isEmpty("") = true * StringUtils.isEmpty(" ") = false * StringUtils.isEmpty("bob") = false * StringUtils.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); } }