- 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 can we check if specific string occurs multiple times in another string in Java?
You can find whether a String contains a specified sequence of characters using any of the methods −
The indexOf() method − The indexOf() method of the String class accepts a string value and finds the (starting) index of it in the current String and returns it. This method returns -1 if it doesn’t find the given string in the current one.
The contains() method − The contains a () method of the String class accepts a sequence of characters value and verifies whether it exists in the current String. If found it returns true else it returns false.
In addition to these, you can also use the Split() method of the String class. This method accepts a String value representing a delimiter, splits the current String based on the given delimiter and returns a String array containing tokens of the String.
You can split the string into an array of words using this method and compare each word with the required word manually.
Example
Following Java example reads the contents of a file into a String, accepts a word from the user and prints the number of times the given word occurred in the String (file).
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class StringOccurrence { public static String fileToString(String filePath){ Scanner sc = null; String input = null; StringBuffer sb = null; try { sc = new Scanner(new File(filePath)); sb = new StringBuffer(); while (sc.hasNextLine()) { input = sc.nextLine(); sb.append(" "+input); } } catch(Exception ex) { ex.toString(); } System.out.println("Contents of the file: "); System.out.println(sb); return sb.toString(); } public static void main(String args[]) throws FileNotFoundException { //Reading the word to be found from the user String filePath = "D://sampleData.txt"; Scanner sc = new Scanner(System.in); System.out.println("Enter the word to be found"); String word = sc.next(); boolean flag = false; int count = 0; String text = fileToString(filePath); String textArray[] = text.split(" "); for(int i=0; i<textArray.length; i++) { if(textArray[i].equals(word)) { flag = true; count = count+1; } } if(flag) { System.out.println("Number of occurrences is: "+count); } else { System.out.println("File does not contain the specified word"); } } }
Output
Enter the word to be found Readers Contents of the file: Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms. The journey commenced with a single tutorial on HTML in 2006 and elated by the response it generated, we worked our way to adding fresh tutorials to our repository which now proudly flaunts a wealth of tutorials and allied articles on topics ranging from programming languages to web designing to academics and much more. 40 million readers read 100 million pages every month. Our content and resources are freely available and we prefer to keep it that way to encourage our readers acquire as many skills as they would like to. We don’t force our readers to sign up with us or submit their details either. No preconditions and no impediments. Simply Easy Learning! Number of occurrences is: 4
- Related Articles
- Check If a String Can Break Another String in C++
- How to check if multiple strings exist in another string in Python?
- How can we check an underflow occurs in Java?
- Check if string contains another string in Swift
- Check if a string can be repeated to make another string in Python
- In MySQL, how can we pad a string with another string?
- How to check if a String contains another String in a case insensitive manner in Java?
- How can we replace specific part of String and StringBuffer in Java?
- How to check if the string begins with specific substring in Java?
- How to check if the string ends with specific substring in Java?
- Check if a string can be formed from another string using given constraints in Python
- Check if a string can be obtained by rotating another string 2 places in Python
- How to check if a string contains a specific sub string?
- In MySQL, how can we check whether a string of a specified pattern is not present within another string?
- Write a program in Java to check if a string can be obtained by rotating another string by 2 places
