CodeGym /Java Blog /Random-IT /Metodo lunghezza stringa()
John Squirrels
Livello 41
San Francisco

Metodo lunghezza stringa()

Pubblicato nel gruppo Random-IT

Qual è la lunghezza di una stringa?

“La lunghezza di una stringa in Java è uguale al numero di caratteri in essa contenuti. Compresi alfabeti, cifre, spazi e altri caratteri speciali."
Ad esempio, la lunghezza della stringa “ Ehi, finalmente è estate! " è 26. Metodo lunghezza stringa() - 1

Come trovare la lunghezza di una stringa in Java?

Java fornisce un metodo semplice e pratico per calcolare la lunghezza di qualsiasi String . Senza ulteriori indugi, diamo un'occhiata a un esempio per trovare la lunghezza della stringa .

Esempio

public class StringLength {
    public static void main(String args[]) {

      String alphabetsWithSpaces = "A B C";
      String digitsWithSpaces = "1 2 3";
      String specialCharsWithSpaces = "! @ $";
      String allChars = "Z X      3 $$$ ^^^ * )(";
      String sentence = "Hey, it's finally summers!";

      System.out.println(alphabetsWithSpaces + " length = " + alphabetsWithSpaces.length());
      System.out.println(digitsWithSpaces + " length = " + digitsWithSpaces.length());
      System.out.println(specialCharsWithSpaces + " length = " + specialCharsWithSpaces.length());
      System.out.println(allChars + " length = " + allChars.length());
      System.out.println(sentence + " length = " + sentence.length());
    }
}

Produzione

ABC lunghezza = 5 1 2 3 lunghezza = 5 ! @ $ lunghezza = 5 ZX 3 $$$ ^^^ * )( lunghezza = 23 Ehi, finalmente è estate! lunghezza = 26

Conclusione

A questo punto devi avere familiarità con la ricerca della lunghezza della stringa in Java. Tuttavia, se hai ancora delle ambiguità, non esitare a testarle. Buon apprendimento!
Commenti
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION