logo

Lunghezza stringa Java()

IL Lunghezza classe stringa Java() Il metodo trova la lunghezza di una stringa. La lunghezza della stringa Java è uguale alle unità di codice Unicode della stringa.

Firma

La firma del metodo string length() è riportata di seguito:

 public int length() 

Specificato da

Interfaccia CharSequence

ritorna

Lunghezza dei caratteri. In altre parole, il numero totale di caratteri presenti nella stringa.

Implementazione interna

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

La classe String utilizza internamente un array char[] per memorizzare i caratteri. La variabile lunghezza dell'array viene utilizzata per trovare il numero totale di elementi presenti nell'array. Poiché la classe Java String utilizza internamente questo array char[]; pertanto la lunghezza variabile non può essere esposta al mondo esterno. Pertanto, gli sviluppatori Java hanno creato il metodo length(), che espone il valore della variabile length. Si può anche pensare al metodo length() come al metodo getter(), che fornisce all'utente un valore del campo della classe. L'implementazione interna descrive chiaramente che il metodo length() restituisce il valore della variabile length.

Esempio del metodo Java String length()

Nome del file: LunghezzaEsempio.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 }} 
Provalo adesso

Produzione:

string length is: 10 string length is: 6 

Metodo Java String length() Esempio 2

Poiché il metodo length() fornisce il numero totale di caratteri presenti nella stringa; quindi si può anche verificare se la stringa data è vuota oppure no.

Nome del file: LunghezzaEsempio2.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()); } } }

Produzione:

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

Metodo Java String length() Esempio 3

Il metodo length() viene utilizzato anche per invertire la stringa.

stringa Java su int

Nome del file: LunghezzaEsempio3.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;>

Metodo Java String length() Esempio 4

Il metodo length() può essere utilizzato anche per trovare solo gli spazi bianchi presenti nella stringa. Osservare il seguente esempio.

Nome del file: LunghezzaEsempio4.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); } } 

Produzione:

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