logo

Longueur de la chaîne Java()

Le Longueur de la classe Java String() La méthode trouve la longueur d’une chaîne. La longueur de la chaîne Java est la même que les unités de code Unicode de la chaîne.

Signature

La signature de la méthode string length() est donnée ci-dessous :

 public int length() 

Spécifié par

Interface CharSequence

Retour

Longueur des caractères. Autrement dit, le nombre total de caractères présents dans la chaîne.

Implémentation interne

 public int length() { return value.length; } 

La classe String utilise en interne un tableau char[] pour stocker les caractères. La variable length du tableau est utilisée pour trouver le nombre total d'éléments présents dans le tableau. Puisque la classe Java String utilise ce tableau char[] en interne ; par conséquent, la variable de longueur ne peut pas être exposée au monde extérieur. Par conséquent, les développeurs Java ont créé la méthode length(), qui expose la valeur de la variable length. On peut également considérer la méthode length() comme la méthode getter(), qui fournit une valeur du champ de classe à l'utilisateur. L'implémentation interne montre clairement que la méthode length() renvoie la valeur de la variable length.

Exemple de méthode Java String length()

Nom de fichier: LongueurExemple.java

 public class LengthExample{ public static void main(String args[]){ String s1='javatpoint'; String s2='python'; System.out.println('string length is: '+s1.length());//10 is the length of javatpoint string System.out.println('string length is: '+s2.length());//6 is the length of python string }} 
Testez-le maintenant

Sortir:

string length is: 10 string length is: 6 

Méthode Java String length() Exemple 2

Puisque la méthode length() donne le nombre total de caractères présents dans la chaîne ; par conséquent, on peut également vérifier si la chaîne donnée est vide ou non.

Nom de fichier: LongueurExemple2.java

 public class LengthExample2 { public static void main(String[] args) { String str = 'Javatpoint'; if(str.length()>0) { System.out.println('String is not empty and length is: '+str.length()); } str = ''; if(str.length()==0) { System.out.println('String is empty now: '+str.length()); } } }

Sortir:

String is not empty and length is: 10 String is empty now: 0 

Méthode Java String length() Exemple 3

La méthode length() est également utilisée pour inverser la chaîne.

Nom de fichier: LongueurExemple3.java

 class LengthExample3 { // main method public static void main(String argvs[]) { String str = &apos;Welcome To JavaTpoint&apos;; int size = str.length(); System.out.println(&apos;Reverse of the string: &apos; + &apos;&apos;&apos; + str + &apos;&apos;&apos; + &apos; is&apos;); for(int i = 0; i <size; i++) { printing in reverse order system.out.print(str.charat(str.length() - i 1)); } < pre> <p> <strong>Output:</strong> </p> <pre> Reverse of the string: &apos;Welcome To JavaTpoint&apos; is tniopTavaJ oT emocleW </pre> <h2>Java String length() Method Example 4</h2> <p>The length() method can also be used to find only the white spaces present in the string. Observe the following example.</p> <p> <strong>FileName:</strong> LengthExample4.java</p> <pre> public class LengthExample4 { // main method public static void main(String argvs[]) { String str = &apos; Welcome To JavaTpoint &apos;; int sizeWithWhiteSpaces = str.length(); System.out.println(&apos;In the string: &apos; + &apos;&apos;&apos; + str + &apos;&apos;&apos;); str = str.replace(&apos; &apos;, &apos;&apos;); int sizeWithoutWhiteSpaces = str.length(); // calculating the white spaces int noOfWhieSpaces = sizeWithWhiteSpaces - sizeWithoutWhiteSpaces; System.out.print(&apos;Total number of whitespaces present are: &apos; + noOfWhieSpaces); } } </pre> <p> <strong>Output:</strong> </p> <pre> In the string: &apos; Welcome To JavaTpoint &apos; Total number of whitespaces present are: 4 </pre> <hr></size;>

Méthode Java String length() Exemple 4

La méthode length() peut également être utilisée pour rechercher uniquement les espaces blancs présents dans la chaîne. Observez l’exemple suivant.

Nom de fichier: LongueurExemple4.java

 public class LengthExample4 { // main method public static void main(String argvs[]) { String str = &apos; Welcome To JavaTpoint &apos;; int sizeWithWhiteSpaces = str.length(); System.out.println(&apos;In the string: &apos; + &apos;&apos;&apos; + str + &apos;&apos;&apos;); str = str.replace(&apos; &apos;, &apos;&apos;); int sizeWithoutWhiteSpaces = str.length(); // calculating the white spaces int noOfWhieSpaces = sizeWithWhiteSpaces - sizeWithoutWhiteSpaces; System.out.print(&apos;Total number of whitespaces present are: &apos; + noOfWhieSpaces); } } 

Sortir:

 In the string: &apos; Welcome To JavaTpoint &apos; Total number of whitespaces present are: 4