- 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
Searching characters and substring in a String in Java
Following is the Java program to search characters and substring in a string −
Example
import java.io.*; import java.lang.*; public class Demo { public static void main (String[] args) { String test_str = "Hello"; CharSequence seq = "He"; boolean bool_1 = test_str.contains(seq); System.out.println("Was the substring found? " + bool_1); boolean bool_2 = test_str.contains("Lo"); System.out.println("Was the substring found? " + bool_2); } }
Output
Was the substring found? true Was the substring found? False
A class named Demo contains the main function. Here, a string is defined, and a CharSequence instance is created. The ‘contains’ function associated with the string is used to check if the original string contains the specific substring. It is then printed on the screen.
Advertisements