[Java] 문자열 찾기


public class StringCheck {

	public static void main(String[] args){
		String comment = "No winter lasts forever no spring skips its turn.";
        
        /* contains : 일치하면 true, 일치하지 않으면 false 반환 */
        System.out.println("contains return 1 : "+comment.contains("winter"));
        System.out.println("contains return 2 : "+comment.contains("summer"));
        /* indexof : 일치하면 true, 일치하지 않으면 false 반환 */
        System.out.println("indexOf return 1 : "+ (comment.indexOf("winter") > -1));
        System.out.println("indexOf return 2 : "+ (comment.indexOf("summer") > -1));
        /* equals : 일치하면 true, 일치하지 않으면 false 반환 */
        System.out.println("equals return 1 : "+ comment.equals("No winter lasts forever no spring skips its turn."));
        System.out.println("equals return 2 : "+ comment.equals("winter lasts forever"));
        /* matches : 일치하면 true, 일치하지 않으면 false 반환 */
        System.out.println("matches return 1 : "+ comment.matches(".*spring skips its turn.*"));
        System.out.println("matches return 2 : "+ comment.matches("spring skips"));
	}        
}

결과값

contains return 1 : true
contains return 2 : false
indexOf return 1 : true
indexOf return 2 : false
equals return 1 : true
equals return 2 : false
matches return 1 : true
matches return 2 : false

출처 : https://bono915.tistory.com/entry/JAVA-%EB%AC%B8%EC%9E%90-%EB%B9%84%EA%B5%90contains-indexOf-equals-matches

좋은 웹페이지 즐겨찾기