首页 > 解决方案 > 如果 s(a string) 不包含 chars(another string) 中找到的任何字符,您如何编写返回 true 的代码,否则返回 false

问题描述

如果 s(a string) 不包含 chars(another string) 中找到的任何字符,您如何编写返回 true 的代码,否则返回 false?

char[] original = s.toCharArray(); 


char [] sub = chars.toCharArray();

for(int i = 0; i <s.length(); i++)

{

if (s.contains(chars)

{

return true;

}

}

return false;

标签: java

解决方案


试试下面的代码,结果是假的,是真的。

public class Main {
    public static void main(String[] args) {
        System.out.println(isNotContained("abc", "dc"));
        System.out.println(isNotContained("abc", "de"));
    }

    private static boolean isNotContained(final String source, final String target) {
        char[] original = source.toCharArray();
        for (char c : original) {
            if (target.contains(String.valueOf(c))) {
                return false;
            }
        }
        return true;
    }
}

推荐阅读