TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java String Occurrence Method: While Loop Method

Count occurrences of a string within another string. Use a while-loop and indexOf.
String occurrences. A string may occur many times within another string. We can count these occurrences with a while-loop and the indexOf method.StringsWhile
With indexOf, we search for the next occurrence in the string. We increment a count variable to keep track of matches. This method is efficient.indexOf
Example code. Here we introduce the countStringOccurrences method. This method receives two parameters. It returns the count of one string within another.

Argument 1: This is the source string we want to search within. We name this argument "text."

Argument 2: This is the string we want to find—the pattern within the first string. We name this argument "pattern."

Main: Here we test the countStringOccurrences method. We can check the results with a sample string.

Java program that counts string occurrences public class Program { public static int countStringOccurrences(String text, String pattern) { int count = 0; int i = 0; // Keep calling indexOf for the pattern. while ((i = text.indexOf(pattern, i)) != -1) { // Advance starting index. i += pattern.length(); // Increment count. count++; } return count; } public static void main(String[] args) { String value = "cat dog dog bird"; // Test method on these strings. int count = countStringOccurrences(value, "dog"); System.out.println("dog occurs: " + count); System.out.println("dirt occurs: " + countStringOccurrences(value, "dirt")); System.out.println("bird occurs: " + countStringOccurrences(value, "bird")); System.out.println("[ d] occurs: " + countStringOccurrences(value, " d")); } } Output dog occurs: 2 dirt occurs: 0 bird occurs: 1 [ d] occurs: 2
Some details. There are some subtle details in the countStringOccurrences method. We increment the variable i the length of the pattern on a match.

And: This step reduces unnecessary searching. It also means overlapping patterns are skipped.

Caution: To find overlapping patterns within the source string, please change the method to increment "i" by 1.

Performance. For optimal performance, a Boyer-Moore string search algorithm could be applied here. And sometimes we can adjust programs so that no searching is needed.
A review. This method counts occurrences of a string in another string. No count() method is available on String in Java. This method can be helpful when one is needed.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf