java - 如果 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;
解决方案
试试下面的代码,结果是假的,是真的。
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;
}
}
推荐阅读
- javascript - 数组返回0长度,无法在html中调用
- python - 使用aiohttp获取多个网站的状态
- html - 带有框阴影css的边框半径
- python - 在 python 函数中使用字符串时接受“接近匹配”?
- solr - 索引 Solr 时如何指定文件类型
- node.js - MongoDb + 猫鼬 | 如何散列密码数组而不是单个密码
- node.js - 为什么 vscode 控制台显示调试失败,即使它没有?
- css - 在测试中验证文本的 CSS 文本转换
- python - 如何从我的字典值中删除 '\n'
- python - 加载数据集时“没有足够的价值来解压” - Allennlp _read