코드지우개

contains(), indexof() 본문

java

contains(), indexof()

코드지우개 2023. 3. 7. 21:33
반응형
contains()

contains() : 문자열이나 List에 특정 문자열이 포함되어 있다면 true 없다면 false 리턴 (대문자, 소문자를 구분한다)

//문자열
String str = "HELLO world";
System.out.println( str.contains("world") ); // 출력 : true
System.out.println( str.contains("hello") ); // 출력 : false
----------------------------------------------------------------
//List
List<String> list = new ArrayList<>();
list.add("빨강");
list.add("노랑");
list.add("파랑");
list.add("초록");
System.out.println( list.contains("빨강") ); // 출력 : true
System.out.println( list.contains("보라") ); // 출력 : false
 

indexof()

indexof( "찾을 특정 문자", "시작할 위치" ) : 특정 문자나 문자열이 앞에서부터 처음 발견되는 인덱스를 반환한다 만약 찾이 못했을 경우 -1을 반환

String str = "hello world";
System.out.println( str.indexof("e") ); // 출력 : 1
System.out.println( str.indexof("l") ); // 출력 : 2
System.out.println( str.indexof("f") ); // 출력 : -1
System.out.println( str.indexof("l", 5) ); // 출력 : 10
 
반응형

'java' 카테고리의 다른 글

추상 클래스  (0) 2023.03.30
다형성과 상속  (0) 2023.03.10
HashMap에서 Key, Value 꺼내기  (0) 2023.03.10
split() : 문자열 -> 배열, join() : 배열 -> 문자열  (0) 2023.03.07
StringUtils 문자열/공백 처리  (0) 2023.03.07
Comments