자바 substr count()

Jmnote (토론 | 기여)님의 2021년 4월 14일 (수) 18:56 판 (→‎개요)

1 개요

자바 substr_count()
import java.util.regex.Pattern;
public class MyClass {
    public static void main(String args[]) {
        String s = "This is a test";
        String sub = "is";
        int count = s.split(sub,-1).length-1;
        System.out.println( count ); // 2
    }
}
import java.util.regex.Pattern;
public class MyClass {
    public static void main(String args[]) {
        String s = "This is a test";
        String sub = "is";
        int count = s.split(Pattern.quote(sub), -1).length-1;
        System.out.println( count ); // 2
    }
}
public class MyClass {
    public static void main(String args[]) {
        String s = "This is a test";
        String sub = "is";
        int count = (s.length()-haystack.replace(sub,"").length())/sub.length();
        System.out.println( count ); // 2
    }
}

2 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}