效率(1) : String.indexOf與String.contains效率測試_string contains效率-CSDN博客
結論是前者效率高,源碼里面conatins是使用indexof?在jdk8中contains直接調用的indexOf(其他版本沒有驗證),所以要說效率來說肯定是indexOf高,但contains也就多了一層方法棧,so 什么方便用什么,,沒必要糾結這個
-
/**
-
* Returns true if and only if this string contains the specified
-
* sequence of char values.
-
*
-
* @param s the sequence to search for
-
* @return true if this string contains {@code s}, false otherwise
-
* @since 1.5
-
*/
-
public boolean contains(CharSequence s) {
-
return indexOf(s.toString()) > -1;
-
}
java大量對比字符串內容是否存在 contains和正則誰更快_string contains 性能-CSDN博客
正則慢
字符串中判斷存在的幾種模式和效率(string.contains、string.IndexOf、Regex.Match)-CSDN博客?