산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> java

예제가 있는 Java 문자열 charAt() 메서드

자바 문자열 charAt() 메소드란 무엇입니까?

자바 문자열 charAt() 메서드는 문자열에서 명확한 인덱스에 있는 문자를 반환합니다. 이 Java 메서드에서 문자열 인덱스 값은 0에서 시작하여 문자열 길이에서 1(n-1)을 뺀 값까지 증가합니다.

charAt() 메서드 구문

public char charAt(int index)

charAt() 메서드에 대한 매개변수 입력

색인 – 이 Java 메소드는 int 데이터 유형인 단일 입력만 허용합니다.

자바 문자열 charAt() 메소드의 반환 유형

Java String charAt() 메서드는 인덱스 입력을 기반으로 문자 유형 데이터를 반환합니다.

예외

인덱스 값이 0과 문자열 길이에서 1을 뺀 값 사이가 아니면 java.lang.StringIndexOutOfBoundsException을 던집니다.

자바 문자열 charAt() 메소드의 예

public class CharAtGuru99 {
    public static void main(String args[]) {
        String s1 = "This is String CharAt Method";
        //returns the char value at the 0 index
        System.out.println("Character at 0 position is: " + s1.charAt(0));
        //returns the char value at the 5th index
        System.out.println("Character at 5th position is: " + s1.charAt(5));
        //returns the char value at the 22nd index
        System.out.println("Character at 22nd position is: " + s1.charAt(22));
        //returns the char value at the 23th index
        char result = s1.charAt(-1);
        System.out.println("Character at 23th position is: " + result);
    }
}

출력:

0 위치의 문자:T
5번째 위치의 문자:i
22번째 위치의 문자:M

스레드 "main" 예외 java.lang.StringIndexOutOfBoundsException:문자열 인덱스가 범위를 벗어남:-1

Java charAt() 메서드에 대한 몇 가지 중요한 사항:


java

  1. 자바 문자열
  2. Java의 String Length() 메서드:예제로 찾는 방법
  3. 하위 문자열 및 예제가 있는 Java 문자열 indexOf() 메서드
  4. Java String compareTo() 메서드:예제와 함께 사용하는 방법
  5. Java 문자열 contains() 메소드 | 예제로 하위 문자열 확인
  6. 예제가 포함된 Java 문자열 endWith() 메서드
  7. Java 문자열 replace(), replaceAll() 및 replaceFirst() 메서드
  8. 프로그램 예제가 있는 Java의 삽입 정렬 알고리즘
  9. EXAMPLE이 있는 Python String strip() 함수
  10. 파이썬 문자열 길이 | len() 메서드 예제