String.contains()方法
关于String的方法String.contains()。
·
判断该String对象中是否包含参数中的字符串,存在返回true,否则返回false(此判断严格区分大小写)。
String test = "Hello World";
Boolen type1 = test.contains("ell");//type = true
Boolen type2 = test.contains("World");//type = true
Boolen type3 = test.contains("world");//type = false
如果strA中不包括strB,使用strA.Contains(strB)更优;反之,如果strA中包括strB,使用strA.IndexOf(strB)更优。(Regex.Match在此方法中貌似没有体现出任何优势,它更适用于模糊匹配)
具体要使用string.Contains,或是string.IndexOf要看形势。
string.Contains是基于string.IndexOf上的一个方法,使用string.Contains的时候,会调用string.IndexOf,按原理,使用string.IndexOf的效率是要高于string.Contains的,但是还需要根据具体的业务可能的情况去选择
更多推荐




所有评论(0)