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

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

주제

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

함수>

indexOf(String str)

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

사용예

String seasonGoods[] = new String[]{"봄꽃", "여름바다", "가을단풍", "겨울눈"}; String str = ""; for (int i = 0; i < seasonGoods.length; i++) { if (seasonGoods[i].indexOf("가을") > -1) { str = seasonGoods[i]; break; } } // 결과: 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

from http://withthisclue.tistory.com/533 by ccl(A) rewrite - 2021-12-18 05:28:16