Supprimer les Espaces de début et de fin de la chaîne Java


Existe-t-il une méthode pratique pour supprimer les espaces de début ou de fin d'une chaîne Java?

Quelque Chose comme:

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

Résultat:

no spaces:keep this

myString.replace(" ","") de remplacer l'espace entre garder et ce.

Author: TylerH, 2011-07-11

7 answers

Vous pouvez essayer la méthode trim ().

String newString = oldString.trim();

Prendre un coup d'oeil à javadocs

 608
Author: woliveirajr, 2014-11-12 06:06:35

Utilisation String#trim() méthode ou String allRemoved = myString.replaceAll("^\\s+|\\s+$", "") pour couper à la fois la fin.

Pour le trim gauche:

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

Pour la garniture droite:

String rightRemoved = myString.replaceAll("\\s+$", "");
 82
Author: Prince John Wesley, 2016-07-13 11:09:24

De la docs:

String.trim();
 32
Author: Richard H, 2011-07-11 15:40:16

Trim() est votre choix, mais si vous voulez utiliser la méthode replace which qui pourrait être plus flexible, vous pouvez essayer ce qui suit:

String stripppedString = myString.replaceAll("(^ )|( $)", "");
 18
Author: James.Xu, 2011-07-11 15:42:56

Avec Java-11 et supérieur, vous pouvez utiliser String.strip API pour renvoyer une chaîne dont la valeur est cette chaîne, avec tous les espaces de début et de fin supprimés. Le javadoc pour le même lit:

/**
 * 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()

Les exemples de cas pour ceux-ci pourraient être: {

System.out.println("  leading".strip()); // prints "leading"
System.out.println("trailing  ".strip()); // prints "trailing"
System.out.println("  keep this  ".strip()); // prints "keep this"
 4
Author: Naman, 2019-11-06 11:05:00

Pour couper un caractère spécifique, vous pouvez utiliser:

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

Ici supprimera l'espace de début et de fin et la virgule .

 0
Author: Galley, 2019-01-03 07:17:00

S. strip() vous pouvez utiliser à partir de java 11.

S. trim() vous pouvez utiliser.

 0
Author: Satya, 2020-10-03 10:04:03