- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to check if the String contains any character in the given set of characters
Here, we have a string.
String str = "abcde";
In the above string, we want to search for the following set of characters.
// set of characters to be searched char[] chSearch = {'b', 'c'};
For this, loop through the length of the string and check every character in the string str. If the matching character from “chSearch” is found in the string, then it would be success.
The following is an example.
Example
public class Demo { public static void main(String[] args) { String str = "abcde"; // set of characters to be searched char[] chSearch = {'b', 'c'}; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); for (int j = 0; j < chSearch.length; j++) { if (chSearch[j] == ch) { System.out.println("Character "+chSearch[j]+" found in string "+str); } } } } }
Output
Character b found in string abcde Character c found in string abcde
Advertisements