etc

[Java] 문자열에서 원하는 단어 찾기 - indexOf()

이단서로 2021. 12. 18. 05:04

 

 

 주제

 

문자열에서 원하는 단어 찾기

 

함수>

indexOf(String str)

: 문자열이 처음 나타나는 이 문자열 내의 인덱스를 반환합니다

 

 

 사용예

 

        // indexOf 사용
        String seasonGoods[] = new String[]{"봄꽃", "여름바다", "가을단풍", "겨울눈"};
        String str = "";
        for (int i = 0; i < seasonGoods.length; i++) {
            if (seasonGoods[i].indexOf("가을") > -1) {
                str = seasonGoods[i];
                System.out.println("str: " + str);
            }
        }
        
        // 출력: str: 가을

 

 

 출처

 

Class String

 

Method Summary

Modifier and Type Method and Description
int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

 

 

728x90