方法一:indexOf(String s)
可以配合indexOf(String s)的使用,如果包含,返回的值是包含该子字符串在父类字符串中起始位置;如果不包含必定全部返回值为-1
public void test02() {
String str1="张三";
String str2="是一个张三大笨蛋";
if(str2.indexOf(str1)!=-1) {
System.out.println("存在包含关系");
}else {
System.out.println("不存在包含关系");
}
}
方法二:contains()
String类型有一个方法:contains(),该方法是判断字符串中是否有子字符串。如果有则返回true,如果没有则返回false。
package api.api;
public class App1 {
public static void main(String[] args) {
String num = "WKCON190400111";
if (num.contains("CON")) {
System.out.println(1);
} else {undefined
System.out.println(2);
}
}
}
输出结果:
1
String.indexOf与String.contains效率测试
String.indexOf与String.contains都是判断字符串是否包含另一个字符串的方法。String.indexOf存在返回第一个字符索引位置,不存才返回-1;String.contains存在返回true,不存在返回false。
现在测试两个方法的效率,不区分大小写判断。一个字符串判断是否含有48个单词中的单词,执行一百万次。
结论 : String.indexOf 效率更高。 (或许我的测试方法有误,欢迎指正)
先上结果,如下
1000000*48 次 String.contains 耗时:4691 ms
1000000*48 次 String.indexOf 耗时:31 ms
^str1:全部存在(小写)-------------------------------------------------------------------
1000000*48 次 String.contains 耗时:3735 ms
1000000*48 次 String.indexOf 耗时:17 ms
^str2:全部存在(大写)-------------------------------------------------------------------
1000000*48 次 String.contains 耗时:37 ms
1000000*48 次 String.indexOf 耗时:14 ms
^str3:部分存在------------------------------------------------------------------------
1000000*48 次 String.contains 耗时:17 ms
1000000*48 次 String.indexOf 耗时:14 ms
^str4:不存在(数字、字符、字母)----------------------------------------------------------
1000000*48 次 String.contains 耗时:16 ms
1000000*48 次 String.indexOf 耗时:16 ms
^str5:不存在(字母)--------------------------------------------------------------------
1000000*48 次 String.contains 耗时:17 ms
1000000*48 次 String.indexOf 耗时:14 ms
^str6:不存在(数字)--------------------------------------------------------------------
1000000*48 次 String.contains 耗时:17 ms
1000000*48 次 String.indexOf 耗时:14 ms
^str7:不存在(字符)--------------------------------------------------------------------
测试代码如下
public class StringContainsOrIndexOfVelocity {
private static final String base = "article.add,article.update,article.delete,article.view," +
"category.add,category.update,category.delete,category.view," +
"user.add,user.update,user.delete,user.view," +
"role.add,role.update,role.delete,role.view," +
"news.add,news.update,news.delete,news.view," +
"category.add,category.update,category.delete,category.view," +
"Threading.add,Threading.update,Threading.delete,Threading.view," +
"System.add,System.update,System.delete,System.view," +
"Generic.add,Generic.update,Generic.delete,Generic.view," +
"Collections.add,Collections.update,Collections.delete,Collections.view," +
"Tasks.add,Tasks.update,Tasks.delete,Tasks.view," +
"CslApp.add,CslApp.update,CslApp.delete,CslApp.view";
private static final String[] bases = base.toUpperCase().split(",");
private static final String str1 = base;
private static final String str2 = base.toUpperCase();
private static final String str3 = "ns.add,Collections.update,Collections.delete,Collections.ving.add,Threading.update,Threading.delete,Threading.add,role.update,role.delete,role.vietions.1";
private static final String str4 = "klsjflsjdfoadsfi;jadls;fjoashgewnaiefahefoia274923472395674358920374uy3fh2f92yfh02ydsuhawe9f''''''//>>>>><<<~~~~!!@#$%^&&hrhfa8fewy7rt23984g23fhaiouf";
private static final String str5 = "skdhlskdjfasd;faioegneohfaeofjaoeihfjaopihfeoasmgvlandihfawieughfyoiebfinlkjdsfoiajheiohioejfaesafkhjasdhflakdjsfasdfaldfadfsdjflsfjaldfjlajdflajldjflkaf";
private static final String str6 = "239875824368517230941327041230740471674083295738473297589327492347239567435892037409827304203406324672937027394623764170324671324891748192374937489273489";
private static final String str7 = "`--==-`-=-=-`=-`=-`==-@#$%^&*((*&^%$#$#$@#@##!@#$$%&^*()()())>><:;'''''''//>>>>><<<~~~~!!@#$%^&&*@#$%^&*()#$%^&*(*&^%$#@#$%^&*(*&^%$#@!@#$%^&*(*&^%$#";
public static void main(String[] args) {
int count = 1000000;
test(str1, count);
System.out.println("^str1:全部存在(小写)-------------------------------------------------------------------");
System.out.println();
test(str2, count);
System.out.println("^str2:全部存在(大写)-------------------------------------------------------------------");
System.out.println();
test(str3, count);
System.out.println("^str3:部分存在------------------------------------------------------------------------");
System.out.println();
test(str4, count);
System.out.println("^str4:不存在(数字、字符、字母)----------------------------------------------------------");
System.out.println();
test(str5, count);
System.out.println("^str5:不存在(字母)--------------------------------------------------------------------");
System.out.println();
test(str6, count);
System.out.println("^str6:不存在(数字)--------------------------------------------------------------------");
System.out.println();
test(str7, count);
System.out.println("^str7:不存在(字符)--------------------------------------------------------------------");
}
private static void test(String str, int count) {
str = str.toUpperCase();
long t = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
for (String s : bases) {
if (str.contains(s)) {
}
}
}
long t2 = System.currentTimeMillis();
System.out.println(count + "*" + bases.length + " 次 String.contains 耗时:" + (t2 - t) + " ms");
for (int i = 0; i < count; i++) {
for (String s : bases) {
if (str.indexOf(s) != -1) {
}
}
}
long t3 = System.currentTimeMillis();
System.out.println(count + "*" + bases.length + " 次 String.indexOf 耗时:" + (t3 - t2) + " ms");
}
}
List.contains和String.contains的区别
如果不把List里的元素遍历出来再用contains过滤关键字,直接用List.contains其效果其实是底层遍历List会用equals去匹配,效果是严格的判相等的方法。
而String.contains是用数组去截取每段内容轮流匹配,类似于模糊查询的效果。