Java | trim()

La méthode trim() supprime les espaces de début et de fin.
 

Syntaxe
public String trim()

 
 

Paramètres

La fonction ne reçoit aucun argument.
 

Valeur de retour

La fonction renvoie une chaîne, qui est une copie de la chaîne, sans espace de début et de fin.
 

Exemple :

Le code suivant supprime les espaces de début et de fin:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
String str = " WayToLearnX ";
System.out.println(str);
System.out.println(str.trim());
}
}
public class Main { public static void main(String[] args) { String str = " WayToLearnX "; System.out.println(str); System.out.println(str.trim()); } }
public class Main {
	public static void main(String[] args) {
		String str = "       WayToLearnX       ";
		System.out.println(str);
		System.out.println(str.trim());
	}
}

 
Sortie:

       WayToLearnX       
WayToLearnX
QCM-Java

Laisser un commentaire

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