¿Cuál es la longitud de una cuerda?
¿Cómo encontrar la longitud de una cadena en Java?
Java proporciona un método sencillo y práctico para calcular la longitud de cualquier String . Sin más preámbulos, veamos un ejemplo de cómo encontrar la longitud de una cadena .Ejemplo
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());
}
}
Producción
ABC longitud = 5 1 2 3 longitud = 5 ! @ $ longitud = 5 ZX 3 $$$ ^^^ * )( longitud = 23 ¡Oye, por fin son veranos! longitud = 26
GO TO FULL VERSION