- Trending Categories
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 only certain characters
The following is our string.
String str = "pqrst";
In the above string, we want to search for the following set of characters.
// set of characters to be searched char[] chSearch = {'p', 'q', r'};
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 a success.
The following is an example.
Example
public class Demo { public static void main(String[] args) { String str = "pqrst"; // characters to be searched char[] chSearch = {'p', 'q', 'r'}; 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 p found in string pqrst Character q found in string pqrst Character r found in string pqrst
- Related Articles
- How to check if a string only contains certain characters in Python?
- Python Program to check if String contains only Defined Characters using Regex
- How to check if a string contains only decimal characters?
- Check whether the String contains only digit characters in Java
- How to check if a unicode string contains only numeric characters in Python?
- Check if the String contains only unicode letters in Java
- Python program to check if a string contains all unique characters
- Java Program to check if the String contains any character in the given set of characters
- Java Program to validate if a String contains only numbers
- Check if the String contains only unicode letters and space in Java
- Check if the String contains only unicode letters or digits in Java
- Check if string contains special characters in Swift
- Check if a string contains only alphabets in Java using Regex
- Java Program to Check if a string contains a substring
- Is it possible to check if a String only contains ASCII in java?

Advertisements