logo

Lungimea șirului Java ()

The Lungimea clasei Java String() metoda găsește lungimea unui șir. Lungimea șirului Java este aceeași cu unitățile de cod Unicode ale șirului.

Semnătură

Semnătura metodei string length() este dată mai jos:

 public int length() 

Specificat de

Interfața CharSequence

Se intoarce

Lungimea caracterelor. Cu alte cuvinte, numărul total de caractere prezente în șir.

Implementare internă

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

Clasa String utilizează intern o matrice char[] pentru a stoca caracterele. Variabila de lungime a matricei este utilizată pentru a găsi numărul total de elemente prezente în matrice. Deoarece clasa Java String folosește această matrice char[] intern; prin urmare, variabila lungime nu poate fi expusă lumii exterioare. Prin urmare, dezvoltatorii Java au creat metoda length(), expune valoarea variabilei lungime. De asemenea, se poate gândi la metoda length() ca la metoda getter(), care oferă utilizatorului o valoare a câmpului de clasă. Implementarea internă descrie clar că metoda length() returnează valoarea variabilei lungime.

Exemplu de metodă Java String length().

Nume de fișier: LengthExample.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 }} 
Testează-l acum

Ieșire:

string length is: 10 string length is: 6 

Metoda Java String length() Exemplul 2

Deoarece metoda length() dă numărul total de caractere prezente în șir; prin urmare, se poate verifica și dacă șirul dat este gol sau nu.

Nume de fișier: LengthExample2.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()); } } }

Ieșire:

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

Metoda Java String length() Exemplul 3

Metoda length() este folosită și pentru a inversa șirul.

Nume de fișier: LengthExample3.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;>

Metoda Java String length() Exemplul 4

Metoda length() poate fi folosită și pentru a găsi doar spațiile albe prezente în șir. Observați următorul exemplu.

Nume de fișier: LengthExample4.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); } } 

Ieșire:

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