- 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.
- Related Articles
- Searching characters in a String in Java.
- Largest Substring Between Two Equal Characters in a string in JavaScript
- Extracting a substring as an array of characters in Java
- Java Program to locate a substring in a string
- Find the longest substring with k unique characters in a given string in Python
- Swapping Characters of a String in Java
- Remove newline, space and tab characters from a string in Java
- Java String substring() Method example.
- Program to count number of distinct characters of every substring of a string in Python
- Java Program to find duplicate characters in a String?
- Displaying at most 10 characters in a string in Java
- Convert a String to a List of Characters in Java
- MySQL query to get a substring from a string except the last three characters?
- Longest Substring Without Repeating Characters in Python
- Java program to count upper and lower case characters in a given string

Advertisements