- 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
How to check whether a String contains a substring or not?
This example shows how we can search a word within a String object using indexOf() method which returns a position index of a word within the string if found. Otherwise, it returns -1.
Example
public class SearchStringEmp { public static void main(String[] args) { String strOrig = "Hello readers"; int intIndex = strOrig.indexOf("Hello"); if(intIndex == - 1) { System.out.println("Hello not found"); } else { System.out.println("Found Hello at index " + intIndex); } } }
Output
The above code sample will produce the following result.
Found Hello at index 0
- Related Articles
- How to check whether a string contains a substring in JavaScript?
- How to check whether a string contains a substring in jQuery?
- Golang Program to check a string contains a specified substring or not
- How to check if a string contains a substring in Golang?
- How to check whether an input string contains a specified substring ignoring the case using JSP?
- Java Program to Check if a string contains a substring
- Golang program to check if a string contains a substring
- Check if a String Contains a Substring in Linux
- How to check whether a vector contains an NA value or not in R?
- How to Check Whether a String is Palindrome or Not using Python?
- Check whether a Hashtable contains a specific key or not in C#
- How to check if string or a substring of string starts with substring in Python?
- PHP 8 – Using str_contains() to check if a string contains a substring
- How to check if an input string contains a specified substring in JSP?
- Check whether the Dictionary contains a specific value or not in C#

Advertisements