String의 길이는 얼마일까요?
텍스트는 한 글자부터 문단 전체까지 다양한 길이를 가질 수 있어요. String에 들어 있는 문자의 개수를 아는 것은 유효성 검사, 서식 지정, 텍스트 처리에 꼭 필요합니다. Java에서 String 길이를 어떻게 측정하는지 알아보세요!
이 연습은 강의의 일부입니다
Java 소개
연습 안내
.length()메서드를 사용해bookName변수의 길이를 구하고, 결과를bookNameLength로 저장하세요.- 저자 이름
"Terry Pratchett"의 길이를 구하고, 올바른 기본형으로 저장하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
class HowLong {
public static void main(String[] args) {
String bookName = "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch";
// Find the length of bookName
int bookNameLength = bookName.____();
// Find the length of the String and save it
____ authorNameLength = "Terry Pratchett".____;
System.out.println("The length of the book title is " + bookNameLength);
System.out.println("The length of the author's name is " + authorNameLength);
}
}