8

I want to remove the leading and trailing whitespace from string:

String s = "          Hello World                    ";

I want the result to be like:

s == "Hello world";
jackJoe
  • 11,078
  • 8
  • 49
  • 64
user1037552
  • 259
  • 1
  • 4
  • 10
  • I never thought about how to do such things without predefined methods. Assuming it's possible, then you need to iterate over all the characters in the string, but again you have to use charAt() method of class String. Is it predefined for you? – ka3ak Feb 02 '12 at 08:00
  • possible duplicate of [Removing whitespace from strings in Java](http://stackoverflow.com/questions/5455794/removing-whitespace-from-strings-in-java) – Zala Janaksinh Apr 08 '14 at 09:02
  • possible duplicate of [Strip Leading and Trailing Spaces From Java String](http://stackoverflow.com/questions/6652687/strip-leading-and-trailing-spaces-from-java-string) – woliveirajr Apr 11 '14 at 13:06
  • If you're developing java, you should know two things: 1) http://docs.oracle.com/javase/7/docs/api/ 2) The verb you're looking is to TRIM a string – tremendows Feb 18 '15 at 09:28
  • This is a 100% duplicate. I believe this kind of questions shouldn't be even asked on SO, this shows that an asker didn't even try to do the most basic research. – improbable Sep 27 '19 at 10:30

11 Answers11

36
 s.trim()

see String#trim()

Without any internal method, use regex like

 s.replaceAll("^\\s+", "").replaceAll("\\s+$", "")

or

  s.replaceAll("^\\s+|\\s+$", "")

or just use pattern in pure form

    String s="          Hello World                    ";
    Pattern trimmer = Pattern.compile("^\\s+|\\s+$");
    Matcher m = trimmer.matcher(s);
    StringBuffer out = new StringBuffer();
    while(m.find())
        m.appendReplacement(out, "");
    m.appendTail(out);
    System.out.println(out+"!");
Nishant
  • 54,584
  • 13
  • 112
  • 127
3
String s="Test "; 
s= s.trim();
Zala Janaksinh
  • 2,929
  • 5
  • 32
  • 58
Pratik Patel
  • 474
  • 4
  • 20
2

I prefer not to use regular expressions for trivial problems. This would be a simple option:

public static String trim(final String s) {
    final StringBuilder sb = new StringBuilder(s);
    while (sb.length() > 0 && Character.isWhitespace(sb.charAt(0)))
        sb.deleteCharAt(0); // delete from the beginning
    while (sb.length() > 0 && Character.isWhitespace(sb.charAt(sb.length() - 1)))
        sb.deleteCharAt(sb.length() - 1); // delete from the end
    return sb.toString();
}
xehpuk
  • 7,814
  • 3
  • 30
  • 54
1

Simply use trim(). It only eliminate the start and end excess white spaces of a string.

String fav = " I like apple ";

fav = fav.trim();

System.out.println(fav);

Output: I like apple //no extra space at start and end of the string

Community
  • 1
  • 1
Priya Rajan
  • 687
  • 8
  • 21
1

String.trim() answers the question but was not an option for me. As stated here :

it simply regards anything up to and including U+0020 (the usual space character) as whitespace, and anything above that as non-whitespace.

This results in it trimming the U+0020 space character and all “control code” characters below U+0020 (including the U+0009 tab character), but not the control codes or Unicode space characters that are above that.

I am working with Japanese where we have full-width characters Like this, the full-width space would not be trimmed by String.trim().

I therefore made a function which, like xehpuk's snippet, use Character.isWhitespace(). However, this version is not using a StringBuilder and instead of deleting characters, finds the 2 indexes it needs to take a trimmed substring out of the original String.

public static String trimWhitespace(final String stringToTrim) {
    int endIndex = stringToTrim.length();
    // Return the string if it's empty
    if (endIndex == 0) return stringToTrim;
    int firstIndex = -1;

    // Find first character which is not a whitespace, if any
    // (increment from beginning until either first non whitespace character or end of string)
    while (++firstIndex < endIndex && Character.isWhitespace(stringToTrim.charAt(firstIndex))) { }

    // If firstIndex did not reach end of string, Find last character which is not a whitespace,
    // (decrement from end until last non whitespace character)
    while (--endIndex > firstIndex && Character.isWhitespace(stringToTrim.charAt(endIndex))) { }

    // Return substring using indexes
    return stringToTrim.substring(firstIndex, endIndex + 1);
}
Community
  • 1
  • 1
1

Use the String class trim method. It will remove all leading and trailing whitespace.

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html

T.Ho
  • 1,190
  • 3
  • 11
  • 16
1
String s="          Hello World                    ";
s = s.trim();

For more information See This

Ariful Islam
  • 7,639
  • 7
  • 36
  • 54
0

Since Java 11 String class has strip() method which is used to returns a string whose value is this string, with all leading and trailing white space removed. This is introduced to overcome the problem of trim method.

Docs: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#strip()

Example:

String str = "  abc    ";
// public String strip()
str = str.strip(); // Returns abc

There are two more useful methods in Java 11+ String class:

  1. stripLeading() : Returns a string whose value is this string, with all leading white space removed.

  2. stripTrailing() : Returns a string whose value is this string, with all trailing white space removed.

Shivang Agarwal
  • 1,825
  • 1
  • 14
  • 19
0
s = s.trim();

More info: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()

Why do you not want to use predefined methods? They are usually most efficient.

Tarandeep Gill
  • 1,506
  • 18
  • 34
0

See String#trim() method

hage
  • 5,966
  • 3
  • 32
  • 42
-1

While @xehpuk's method is good if you want to avoid using regex, but it has O(n^2) time complexity. The following solution also avoids regex, but is O(n):

if(s.length() == 0)
    return "";
char left = s.charAt(0);
char right = s.charAt(s.length() - 1);
int leftWhitespace = 0;
int rightWhitespace = 0;
boolean leftBeforeRight = leftWhitespace < s.length() - 1 - rightWhitespace;

while ((left == ' ' || right == ' ') && leftBeforeRight) {
    if(left == ' ') {
        leftWhitespace++;
        left = s.charAt(leftWhitespace);
    }
    if(right == ' ') {
        rightWhitespace++;
        right = s.charAt(s.length() - 1 - rightWhitespace);
    }

    leftBeforeRight = leftWhitespace < s.length() - 1 - rightWhitespace;
}

String result = s.substring(leftWhitespace, s.length() - rightWhitespace);
return result.equals(" ") ? "" : result;

This counts the number of trailing whitespaces in the beginning and end of the string, until either the "left" and "right" indices obtained from whitespace counts meet, or both indices have reached a non-whitespace character. Afterwards, we either return the substring obtained using the whitespace counts, or the empty string if the result is a whitespace (needed to account for all-whitespace strings with odd number of characters).

Cody-G-G
  • 1
  • 1
  • 2
  • Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* and *why* this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Apr 05 '17 at 12:13
  • @TobySpeight added explanation, hope it's useful / enough :). – Cody-G-G Apr 05 '17 at 12:50
  • Slight micro optimization, but wouldn't it be better to simply count until you find a non whitespace character rather than always counting to the middle? – DavidBittner May 30 '17 at 13:23
  • @DavidBittner that's exactly what the code does actually? It doesn't always count to the middle, when both 'first' and 'last' are non-whitespace, the loop exits, which is what the first condition in the while indicates. – Cody-G-G May 31 '17 at 14:26
  • 1
    @DavidBittner updated the code and description to a more clear one, and fixed some issues that I had omitted, hope it's easier to understand now as well :) – Cody-G-G May 31 '17 at 15:34