String one = "This is a test";
String two = "This is a simple test";
我想检查是否two
包含中的所有字符one
,并忽略它包含多余字符的事实。
最快的方法可能是将它们分解为HashSet
s然后应用containsAll
public static Set<Character> stringToCharacterSet(String s) {
Set<Character> set = new HashSet<>();
for (char c : s.toCharArray()) {
set.add(c);
}
return set;
}
public static boolean containsAllChars
(String container, String containee) {
return stringToCharacterSet(container).containsAll
(stringToCharacterSet(containee));
}
public static void main(String[] args) {
String one = "This is a test";
String two = "This is a simple test";
System.out.println (containsAllChars(one, two));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句