ความยาวของสตริงคืออะไร?

จะหาความยาวของ String ใน Java ได้อย่างไร?
Java มีวิธีการที่ง่ายและสะดวกในการคำนวณความยาวของString ใด ๆ เพื่อเป็นการไม่ให้เสียเวลา เรามาดูตัวอย่างการค้นหาความยาว ของ สตริง กันตัวอย่าง
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());
}
}
เอาท์พุต
ความยาว ABC = 5 1 2 3 ความยาว = 5 ! @ $ length = 5 ZX 3 $$$ ^^^ * )( length = 23 เฮ้ ในที่สุดก็ถึงฤดูร้อนแล้ว! length = 26
GO TO FULL VERSION