Strip Leading and Trailing Spaces From Java String

JavaStringReplace

Java Problem Overview


Is there a convenience method to strip any leading or trailing spaces from a Java String?

Something like:

String myString = "  keep this  ";
String stripppedString = myString.strip();
System.out.println("no spaces:" + strippedString);

Result:

no spaces:keep this

myString.replace(" ","") would replace the space between keep and this.

Java Solutions


Solution 1 - Java

You can try the trim() method.

String newString = oldString.trim();

Take a look at javadocs

Solution 2 - Java

Use String#trim() method or String allRemoved = myString.replaceAll("^\\s+|\\s+$", "") for trim both the end.

For left trim:

String leftRemoved = myString.replaceAll("^\\s+", "");

For right trim:

String rightRemoved = myString.replaceAll("\\s+$", "");

Solution 3 - Java

From the docs:

String.trim();

Solution 4 - Java

trim() is your choice, but if you want to use replace method -- which might be more flexiable, you can try the following:

String stripppedString = myString.replaceAll("(^ )|( $)", "");

Solution 5 - Java

With Java-11 and above, you can make use of the String.strip API to return a string whose value is this string, with all leading and trailing whitespace removed. The javadoc for the same reads :

/**
 * Returns a string whose value is this string, with all leading
 * and trailing {@link Character#isWhitespace(int) white space}
 * removed.
 * <p>
 * If this {@code String} object represents an empty string,
 * or if all code points in this string are
 * {@link Character#isWhitespace(int) white space}, then an empty string
 * is returned.
 * <p>
 * Otherwise, returns a substring of this string beginning with the first
 * code point that is not a {@link Character#isWhitespace(int) white space}
 * up to and including the last code point that is not a
 * {@link Character#isWhitespace(int) white space}.
 * <p>
 * This method may be used to strip
 * {@link Character#isWhitespace(int) white space} from
 * the beginning and end of a string.
 *
 * @return  a string whose value is this string, with all leading
 *          and trailing white space removed
 *
 * @see Character#isWhitespace(int)
 *
 * @since 11
 */
public String strip()

The sample cases for these could be:--

System.out.println("  leading".strip()); // prints "leading"
System.out.println("trailing  ".strip()); // prints "trailing"
System.out.println("  keep this  ".strip()); // prints "keep this"

Solution 6 - Java

To trim specific char, you can use:

String s = s.replaceAll("^(,|\\s)*|(,|\\s)*$", "")

Here will strip leading and trailing space and comma.

Solution 7 - Java

s.strip() you can use from java 11 onwards.

s.trim() you can use.

Solution 8 - Java

private void capitaliseEveryWordInASentence() {

    String mm = "Hello there, this is the cluster";

    String[] words = mm.split(" ");
    String outt = "";

    for (String w : words) {

        outt = outt + Character.toUpperCase(w.charAt(0)) + w.substring(1) + " ";
    }

    System.out.println(outt.trim());
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTyler DeWittView Question on Stackoverflow
Solution 1 - JavawoliveirajrView Answer on Stackoverflow
Solution 2 - JavaPrince John WesleyView Answer on Stackoverflow
Solution 3 - JavaRichard HView Answer on Stackoverflow
Solution 4 - JavaJames.XuView Answer on Stackoverflow
Solution 5 - JavaNamanView Answer on Stackoverflow
Solution 6 - JavaGalleyView Answer on Stackoverflow
Solution 7 - JavaSatyaView Answer on Stackoverflow
Solution 8 - JavaXYZetaAndMetaView Answer on Stackoverflow