내풀이
class Solution { public int solution(String str1, String str2) { return str1.contains(str2) ? 1 : 2; } }
📌 indexOf() 메서드란?
Java에서 indexOf() 메서드는 특정 요소 또는 문자열이 배열, 리스트, 문자열에서 처음으로 등장하는 인덱스를 반환하는 메서드입니다.해당 요소가 존재하지 않으면 -1을 반환합니다.
public class Main {
public static void main(String[] args) {
String str = "Hello, world!";
// 문자 찾기
int index1 = str.indexOf('o'); // 'o'가 처음 등장하는 위치
int index2 = str.indexOf("world"); // "world"가 시작되는 위치
int index3 = str.indexOf('z'); // 존재하지 않는 경우
System.out.println(index1); // 출력: 4
System.out.println(index2); // 출력: 7
System.out.println(index3); // 출력: -1
}
}