Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)

Can anyone help me how I can able to check whether an array contains a particular characters or not. I want to do something like this:

if(!charArray contains 'q'){

     break;

}

Any help would be appreciated.

1 Answer

0 votes
by (26.7k points)

Basically, you can use the below code snippet to use the contains method like this:

boolean contains = false;

for (char c : charArray) {

    if (c == 'q') {

        contains = true;

        break;

    }

}

if (!contains) {

    // do something

}

Also, you can use different method like indexOf():

if (new String(charArray).indexOf('q') == -1) {

    // do something

}

I hope this will help.

Want to become a Java Expert? Join Java Certification now!!

Related questions

0 votes
1 answer
0 votes
2 answers
asked Nov 25, 2019 in Java by Anvi (10.2k points)

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...