Qual é o comprimento de uma String?
Como encontrar o comprimento de uma String em Java?
Java fornece um método simples e prático para calcular o comprimento de qualquer String . Sem mais delongas, vejamos um exemplo de como encontrar o comprimento de uma String .Exemplo
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());
}
}
Saída
Comprimento ABC = 5 1 2 3 comprimento = 5 ! @ $ length = 5 ZX 3 $$$ ^^^ * )( length = 23 Ei, finalmente é verão! length = 26