자바 문자열 포함 여부 .contain() 사용법 및 결과값 비교 - 개발새발 작성 일기

칼리와 초콜릿 공장·2023년 10월 28일
1

자바 개념

목록 보기
5/15

.contain() 함수

  • 대상 문자열에 특정 문자열이 포함되어 있는지 확인하는 함수
  • 대/소문자를 구분한다.
public class SampleContains {
    static void containsshow(String str1, String str2) {
        if (str1.contains(str2)) {
            System.out.println('"' + str2 + '"' + " is included in " + '"' + str1 + '"');
        }
        else {
            System.out.println('"' + str2 + '"' + " is not included in " + '"' + str1 + '"');
        }
    }
        public static void main(String[] args) {
        String appleSentence = "apple is good";
        String word1 = "banana";
        String word2 = "is";
        String word3 = "not";
        String word4 = "good";
        containsshow(appleSentence, word1);
        containsshow(appleSentence, word2);
        containsshow(appleSentence, word3);
        containsshow(appleSentence, word4);
    }
}

결과

"banana" is not included in "apple is good"
"is" is included in "apple is good"
"not" is not included in "apple is good"
"good" is included in "apple is good"

profile
바삭하고 촉촉한 초코칩 쿠키

0개의 댓글

관련 채용 정보