java string length method with examples
이 튜토리얼은 개념을 이해하는 데 도움이되는 다중 프로그래밍 예제 및 FAQ와 함께 Java String length () 메소드에 대한 모든 것을 설명합니다.
또한 String Java length () 메서드와 관련된 다양한 시나리오를 다룰 것입니다. Java String length () 메소드와 관련된 자주 묻는 질문도이 튜토리얼의 일부입니다.
이 자습서를 진행하면 문자열의 길이를 계산하고 다양한 경우 또는 시나리오에서 사용할 수 있습니다. 이 메소드는 다른 Java String 메소드와 잘 작동합니다.
=> 여기에서 완벽한 Java 교육 가이드를 확인하십시오.
학습 내용 :
자바 문자열 길이
문자열의 길이는 포함 된 문자 수에 불과합니다. Java에는 문자열의 문자 수를 찾기 위해 length ()라는 내장 메소드가 있습니다.
통사론:
구문은 다음과 같이 제공됩니다.
int length();
여기서 length ()는 문자 수를 찾고 그 결과를 정수 .
문자열의 길이 찾기
이 예에서 , 가장 간단한 형태의 Java String length () 메서드를 다룰 것입니다. 어떤 값으로 문자열을 초기화 한 다음 길이를 계산합니다.
public class length { public static void main(String() args) { // Initialized a String variable String str = 'Testing'; // Initialized a count variable which will store the length int count = str.length(); // Printed the count variable or the length of String. System.out.println('The String has ' +count +' characters'); } }
산출:
문자 배열의 길이 찾기
이 예에서 , 우리는 문자 배열 'chars'를 만든 다음 문자열 변수 'str'에 해당 문자를 병합 한 다음 변수와 길이를 인쇄했습니다.
public class length { public static void main(String() args) { // Initialized a character array char chars() = { 'T', 'e', 's', 't', 'i', 'n', 'g' }; // Initialized a String variable str with chars characters String str = new String(chars); // Printed the String variable System.out.println(str + ' has '); // Printed the length of the String Variable System.out.println(str.length()+ ' characters'); } }
산출:
자바 문자열 길이 시나리오
시나리오 1 : 공백이있는 문자열의 길이 찾기.
설명: 이 시나리오에서는 둘 이상의 단어 또는 하위 문자열이 있고 공백으로 구분 된 문자열의 길이를 찾습니다.
여기에서는 문자로 처리되는 단일 및 이중 공백으로 두 개의 문자열 변수를 초기화했습니다. 그런 다음 길이를 저장할 두 개의 카운트 변수를 초기화했습니다.
마지막으로 카운트 변수를 인쇄했습니다.
자바 인터뷰 코딩 질문 및 답변
public class length { public static void main(String() args) { // Initialized a String variable with a single whitespace String str1 = 'This is'; // Initialized another String variable with two whitespace String str2 = 'Software Testing Help'; /* * Initialized a count1 variable which will store the length of the first String. */ int count1 = str1.length(); /* * Initialized a count2 variable which will store the length of the second String. */ int count2 = str2.length(); // Printed the count1 variable. System.out.println('The First String has ' + count1 + ' characters'); // Printed the count2 variable. System.out.println('The Second String has ' + count2 + ' characters'); } }
산출:
시나리오 2 : 특수 문자가있는 문자열의 길이 찾기.
설명: 여기에서는 특수 문자로 문자열을 초기화하고 문자열의 길이를 가져 오려고합니다.
public class length { public static void main(String() args) { // Initialized a String variable with special characters String str = 'P@!.90$%'; /* * Initialized a count variable which will store the length of the String. */ int count = str.length(); // Printed the count variable. System.out.println('The String has ' + count + ' characters'); } }
산출:
자주 묻는 질문
Q # 1) Java에서 String length ()는 무엇을합니까?
대답: 문자열의 문자 수를 반환합니다. Java의 색인은 0부터 시작하여 n 번째 문자 인 문자열까지 계속됩니다.
길이는 마지막 요소의 인덱스 + 1이됩니다.
예를 들면 :
문자열 str = 'Hello World'
충격파 플래시 개체를 여는 방법
여기서 H는 인덱스 (0)에 있고 e는 인덱스 (1)에 있습니다.
마지막 요소는 index (10)에있는 d입니다. 따라서 총 길이는 11입니다.
질문 # 2) Java에서 문자는 무엇입니까?
대답: 문자는 함께 결합하여 문자열을 형성하는 문자 일뿐입니다. Java는 또한 공백을 문자로 간주합니다. 공백, 특수 문자 등이있는 문자열의 길이를 계산할 때 문자로 처리됩니다.
모든 단일 문자의 크기는 1입니다.
질문 # 3) Java에서 지정된 크기의 문자열을 만드는 방법은 무엇입니까?
대답: 이 프로그램에서는 두 개의 상수를 만들었습니다. 첫 번째 상수는 문자열에서 반복적으로 발생할 문자이고 두 번째 상수는 발생할 횟수입니다. 그런 다음 문자 배열의 모든 요소를 String에 저장했습니다.
나중에 모든 NULL 문자를 첫 번째 상수 문자로 대체했습니다. 마지막으로 문자열을 반환하고 값을 인쇄했습니다.
public class length { // Initialized a constant character which will repeatedly occur static final char chars = '$'; // Specied a constant length limit as 5 static final int StrLen = 5; public static void main(String() args) { // printing the return value of the create method System.out.println(create()); } public static String create(){ //created a new String from the character array String str = new String(new char(StrLen)); //replaced all NULL chars ' ' with specified character $ str = str.replace(' ', chars); return str; } }
산출:
질문 # 4) 문자열의 길이를 변경하는 방법은 무엇입니까?
대답: 아래 프로그램에서는 부분 문자열을 공백으로 대체하여 문자열의 길이를 변경했습니다.
입력 문자열을 가져 와서 문자열과 문자열의 길이를 인쇄했습니다. 그런 다음 기본 문자열의 하위 문자열을 빈 값으로 대체했습니다.
다시, 우리는 문자열과 문자열의 길이를 인쇄했습니다.
public class length { public static void main(String() args) { // Initialized a String variable String str = 'Software Test'; // Printed the String and the length System.out.println(str + ' has ' +str.length()+ ' characters'); // Replaced the substring Test with a blank value str = str.replace(' Test', ''); // Printed the String and the length System.out.println(str + ' has ' +str.length()+ ' characters'); } }
산출:
질문 # 5) Java의 배열 길이는 얼마입니까? String length ()와 어떻게 다른가요?
대답: Array에서 길이는 Array의 길이를 가져 오는 데 사용되는 변수입니다. 우리가해야 할 일은 Array.length를 입력하기 만하면 길이를 알려줍니다.
String에서 length ()는 String의 길이를 가져 오는 데 사용되는 메서드입니다. String.length ()를 넣어 길이를 얻습니다.
아래 프로그램에서 어떻게 작동하는지 살펴 보겠습니다.
public class length { public static void main(String() args) { // Specified the length of an Array as 4. int() arr = new int(4); // returned the length of an Array System.out.println('Array length is ' + arr.length); String str = 'Saket'; // returned the length of the String System.out.println('String length() is ' + str.length()); } }
산출:
결론
이 튜토리얼에서는 Java String length () 메서드를 자세히 이해했습니다. 이것은 원하는 결과를 얻기 위해 다른 String 메서드와 공동으로 사용되는 가장 기본적인 String 메서드입니다.
더 나은 이해를 위해 문자열 길이와 관련된 다양한 사례 또는 시나리오 및 FAQ를 제공했습니다. 이 방법의 기능 영역은 작지만 응용 영역은 다른 방법만큼 큽니다.
이것은 String Class의 가장 간단하고 기본적인 방법입니다.
=> 독점적 인 Java 교육 자습서 시리즈를 보려면 여기를 방문하십시오.
추천 도서
- Java String contains () 메서드 자습서 예제 포함
- 자바 문자열 튜토리얼 | 예제가있는 Java 문자열 메서드
- 프로그래밍 예제가 포함 된 Java String compareTo 메서드
- Java substring () 메서드-예제가 포함 된 자습서
- 문자열 버퍼 및 문자열 작성기 자습서가있는 Java 문자열
- Java String Split () 메서드 – Java에서 문자열을 분할하는 방법
- 초보자를위한 JAVA 튜토리얼 : 100 개 이상의 실습 Java 비디오 튜토리얼
- Java toString 메서드를 사용하는 방법?