D+27-백준.StringIndexOf, Repeat함수

Bku·2024년 2월 7일

학원 일기

목록 보기
29/67
post-thumbnail

StringIndexOf

문자열에서 특정 문자의 위치를 찾아주는 함수이다.
만약에 중복된 문자가 있다면 첫번째 문자 위치만 알려준다.

pblic static void main(String[] args) {
        String str = "abcde";
        int pos = str.indexOf("c");
        System.out.println(pos);

        // 못 찾을 경우 -1이 출력된다.

        int pos2 = str.indexOf("Z");
        System.out.println(pos2);
    }

*결과

문자가 몇번 째 자리에 있는 지를 알려주고, 찾지 못하면 -1을 출력한다.

Repeat함수

public class Repeat {
    public static void main(String[] args) {
        String temp = "hello";
        System.out.println(temp.repeat(5));
    }
}

결과

문자열을 반복할때 사용할 수 있다.
문자열.repeat(반복하고싶은 횟수)를 해주면된다.

profile
기억보단 기록

0개의 댓글