在Java中,我怎么能确定一个字符数组包含特定字符? [英] In Java, how can I determine if a char array contains a particular character?
本文介绍了在Java中,我怎么能确定一个字符数组包含特定字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我有:
char[] charArray = new char[] {'h','e','l','l','o'};
我想要写的东西的效果:
I want to write something to the effect of:
if(!charArray contains 'q'){
break;
}
我知道。载有()不能在这里使用。我只是用包含来说明什么,我试图做的。
I realize that .contains() can't be used here. I am just using "contains" to illustrate what I'm trying to do.
推荐答案
下面的代码片段用于测试的不含有的状况,如例子中的问题样本伪code。对于有明确的循环一个直接的解决方案,做到这一点:
The following snippets test for the "not contains" condition, as exemplified in the sample pseudocode in the question. For a direct solution with explicit looping, do this:
boolean contains = false;
for (char c : charArray) {
if (c == 'q') {
contains = true;
break;
}
}
if (!contains) {
// do something
}
另一种方法,使用的事实,字符串
提供了包含()
方法:
if (!(new String(charArray).contains("q"))) {
// do something
}
另一个选择,这次使用的indexOf()
:
if (new String(charArray).indexOf('q') == -1) {
// do something
}
这篇关于在Java中,我怎么能确定一个字符数组包含特定字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文