Strip Les espaces initiaux et finaux de la chaîne Java

avec Java-11 peuvent désormais utiliser String.strip API pour retourner une chaîne dont la valeur est cette chaîne, avec toutes les espaces vides initiaux et finaux éliminés. Le Javadoc pour les mêmes lectures:

/** * 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 cas d’échantillons 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"

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *