Find Substring Matching In Java: If you want to check if a String contains a certain sub-string, there are several ways to do it in Java. Most of them are similar to what you’d find in other programming languages, but one approach is unique to Java. This involves using the Pattern class, which we’ll cover later in the article.
Substring Matching In Java
The solutions below print the total number of occurrences of the substring throughout the String and include cases where overlapping matches exist Without using predefined methods.
package com.softwaretestingo.interviewprograms;
public class FindMatchingSubstringEx1
{
public static void main(String[] args)
{
String str="abababababaabb";
String sub = "bab";
int n = str.length();
int m = sub.length();
// index=-1 in case of no match, otherwise >=0(first match position)
int index=str.indexOf(sub), i=index+1, count=(index>=0)?1:0;
//System.out.println(i+" "+index+" "+count);
// i will traverse up to only (m-n) position
while(index!=-1 && i<=(n-m))
{
index=str.substring(i, n).indexOf(sub);
count=(index>=0)?count+1:count;
i=i+index+1;
//System.out.println(i+" "+index);
}
System.out.println("Original String: "+str);
System.out.println("Sub String: "+sub);
System.out.println("Total Number Of Substring Appears: "+count);
}
}
package com.softwaretestingo.interviewprograms;
publicclass FindMatchingSubstringEx1
{
publicstaticvoidmain(String[] args)
{
String str="abababababaabb";
String sub = "bab";
int n = str.length();
int m = sub.length();
// index=-1 in case of no match, otherwise >=0(first match position)
int index=str.indexOf(sub), i=index+1, count=(index>=0)?1:0;
//System.out.println(i+" "+index+" "+count);
// i will traverse up to only (m-n) position
while(index!=-1 && i<=(n-m))
{
index=str.substring(i, n).indexOf(sub);
count=(index>=0)?count+1:count;
i=i+index+1;
//System.out.println(i+" "+index);
}
System.out.println("Original String: "+str);
System.out.println("Sub String: "+sub);
System.out.println("Total Number Of Substring Appears: "+count);
}
}
package com.softwaretestingo.interviewprograms;
public class FindMatchingSubstringEx1
{
public static void main(String[] args)
{
String str="abababababaabb";
String sub = "bab";
int n = str.length();
int m = sub.length();
// index=-1 in case of no match, otherwise >=0(first match position)
int index=str.indexOf(sub), i=index+1, count=(index>=0)?1:0;
//System.out.println(i+" "+index+" "+count);
// i will traverse up to only (m-n) position
while(index!=-1 && i<=(n-m))
{
index=str.substring(i, n).indexOf(sub);
count=(index>=0)?count+1:count;
i=i+index+1;
//System.out.println(i+" "+index);
}
System.out.println("Original String: "+str);
System.out.println("Sub String: "+sub);
System.out.println("Total Number Of Substring Appears: "+count);
}
}
Output:
Original String: abababababaabb
Sub String: bab
Total Number Of Substring Appears: 4
Original String: abababababaabb
Sub String: bab
Total Number Of Substring Appears: 4
Original String: abababababaabb
Sub String: bab
Total Number Of Substring Appears: 4
Substring Matching In Java Solution 2:
Here is the advanced version to find substring in string java. By implementing the length() of Java, we can also find out how many times the token occurred when a user entered the string.
package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class FindMatchingSubstringEx2
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a sentence please:");
String str = scanner.nextLine();
System.out.println("Enter a sentence please:");
String sub=scanner.next();
int atIndex = 0;
int count = 0;
while (atIndex != -1)
{
atIndex = str.indexOf(sub, atIndex);
if(atIndex != -1)
{
count++;
atIndex += sub.length();
}
}
System.out.println("Original String: "+str);
System.out.println("Sub String: "+sub);
System.out.println("Total Number Of Substring Appears: "+count);
}
}
package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
publicclass FindMatchingSubstringEx2
{
publicstaticvoidmain(String[] args)
{
Scanner scanner = newScanner(System.in);
System.out.println("Enter a sentence please:");
String str = scanner.nextLine();
System.out.println("Enter a sentence please:");
String sub=scanner.next();
int atIndex = 0;
int count = 0;
while(atIndex != -1)
{
atIndex = str.indexOf(sub, atIndex);
if(atIndex != -1)
{
count++;
atIndex += sub.length();
}
}
System.out.println("Original String: "+str);
System.out.println("Sub String: "+sub);
System.out.println("Total Number Of Substring Appears: "+count);
}
}
package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class FindMatchingSubstringEx2
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a sentence please:");
String str = scanner.nextLine();
System.out.println("Enter a sentence please:");
String sub=scanner.next();
int atIndex = 0;
int count = 0;
while (atIndex != -1)
{
atIndex = str.indexOf(sub, atIndex);
if(atIndex != -1)
{
count++;
atIndex += sub.length();
}
}
System.out.println("Original String: "+str);
System.out.println("Sub String: "+sub);
System.out.println("Total Number Of Substring Appears: "+count);
}
}
Output:
Enter a sentence please: hellohellohello
Enter a sentence please: hello
Original String: hellohellohello
Sub String: hello
Total Number Of Substring Appears: 3
Enter a sentence please: hellohellohello
Enter a sentence please: hello
Original String: hellohellohello
Sub String: hello
Total Number Of Substring Appears: 3
Enter a sentence please: hellohellohello
Enter a sentence please: hello
Original String: hellohellohello
Sub String: hello
Total Number Of Substring Appears: 3
Substring Matching In Java Solution 3:
You can also follow the programs below to find the substring matching in Java easily.
package com.softwaretestingo.interviewprograms;
public class FindMatchingSubstringEx3
{
public static void main(String[] args)
{
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int count = 0, lastIndex = 0;
while((lastIndex = str.indexOf(findStr, lastIndex)) != -1)
{
lastIndex += findStr.length() - 1;
count++;
}
System.out.println("Original String: "+str);
System.out.println("Sub String: "+findStr);
System.out.println("Total Number Of Substring Appears: "+count);
}
}
System.out.println("Total Number Of Substring Appears: "+count);
}
}
package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class FindMatchingSubstringEx5
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a sentence please:");
String str = scanner.nextLine();
System.out.println("Enter a sentence please:");
String sub=scanner.next();
int lastIndex = 0;
int count = 0;
while((lastIndex = str.indexOf(sub, lastIndex)) != -1)
{
count++;
lastIndex += sub.length() - 1;
}
System.out.println("Original String: "+str);
System.out.println("Sub String: "+sub);
System.out.println("Total Number Of Substring Appears: "+count);
}
}
Output:
Enter a sentence please: helloslkhellodjladfjhello
Enter a sentence please: hello
Original String: helloslkhellodjladfjhello
Sub String: hello
Total Number Of Substring Appears: 3
Enter a sentence please: helloslkhellodjladfjhello
Enter a sentence please: hello
Original String: helloslkhellodjladfjhello
Sub String: hello
Total Number Of Substring Appears: 3
Enter a sentence please: helloslkhellodjladfjhello
Enter a sentence please: hello
Original String: helloslkhellodjladfjhello
Sub String: hello
Total Number Of Substring Appears: 3
Java Match Substring Solution 6:
package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class FindMatchingSubstringEx6
{
public static int getCountSubString(String str , String sub)
{
int n = 0, m = 0, counter = 0, counterSub = 0;
while(n < str.length())
{
counter = 0;
m = 0;
while(m < sub.length() && str.charAt(n) == sub.charAt(m))
{
counter++;
m++; n++;
}
if (counter == sub.length())
{
counterSub++;
continue;
}
else if(counter > 0)
{
continue;
}
n++;
}
return counterSub;
}
public static void main(String[] args)
{
int count;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a sentence please:");
String str = scanner.nextLine();
System.out.println("Enter a sentence please:");
String sub=scanner.next();
FindMatchingSubstringEx6 obj=new FindMatchingSubstringEx6();
count=obj.getCountSubString(str, sub);
System.out.println("Original String: "+str);
System.out.println("Sub String: "+sub);
System.out.println("Total Number Of Substring Appears: "+count);
}
}
System.out.println("Total Number Of Substring Appears: "+count);
}
}
package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class FindMatchingSubstringEx6
{
public static int getCountSubString(String str , String sub)
{
int n = 0, m = 0, counter = 0, counterSub = 0;
while(n < str.length())
{
counter = 0;
m = 0;
while(m < sub.length() && str.charAt(n) == sub.charAt(m))
{
counter++;
m++; n++;
}
if (counter == sub.length())
{
counterSub++;
continue;
}
else if(counter > 0)
{
continue;
}
n++;
}
return counterSub;
}
public static void main(String[] args)
{
int count;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a sentence please:");
String str = scanner.nextLine();
System.out.println("Enter a sentence please:");
String sub=scanner.next();
FindMatchingSubstringEx6 obj=new FindMatchingSubstringEx6();
count=obj.getCountSubString(str, sub);
System.out.println("Original String: "+str);
System.out.println("Sub String: "+sub);
System.out.println("Total Number Of Substring Appears: "+count);
}
}
Output:
Enter a sentence please: helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello
Enter a sentence please: hello
Original String: helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello
Sub String: hello
Total Number Of Substring Appears: 7
Enter a sentence please: helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello
Enter a sentence please: hello
Original String: helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello
Sub String: hello
Total Number Of Substring Appears: 7
Enter a sentence please: helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello
Enter a sentence please: hello
Original String: helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello
Sub String: hello
Total Number Of Substring Appears: 7
Find Substring In String Java Solution 7:
here is the other solution without using regexp/patterns/matches or using StringUtils.
package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class FindMatchingSubstringEx7
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a sentence please:");
String str = scanner.nextLine();
System.out.println("Enter a sentence please:");
String sub=scanner.next();
int count =0;
int findStrLength = sub.length();
for(int i=0;i<str.length();i++)
{
if(sub.startsWith(Character.toString(str.charAt(i))))
{
if(str.substring(i).length() >= findStrLength)
{
if(str.substring(i, i+findStrLength).equals(sub))
{
count++;
}
}
}
}
System.out.println("Original String: "+str);
System.out.println("Sub String: "+sub);
System.out.println("Total Number Of Substring Appears: "+count);
}
}
System.out.println("Total Number Of Substring Appears: "+count);
}
}
package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class FindMatchingSubstringEx7
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a sentence please:");
String str = scanner.nextLine();
System.out.println("Enter a sentence please:");
String sub=scanner.next();
int count =0;
int findStrLength = sub.length();
for(int i=0;i<str.length();i++)
{
if(sub.startsWith(Character.toString(str.charAt(i))))
{
if(str.substring(i).length() >= findStrLength)
{
if(str.substring(i, i+findStrLength).equals(sub))
{
count++;
}
}
}
}
System.out.println("Original String: "+str);
System.out.println("Sub String: "+sub);
System.out.println("Total Number Of Substring Appears: "+count);
}
}
Output:
Enter a sentence please: helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello
Enter a sentence please: hello
Original String: helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello
Sub String: hello
Total Number Of Substring Appears: 7
Enter a sentence please: helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello
Enter a sentence please: hello
Original String: helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello
Sub String: hello
Total Number Of Substring Appears: 7
Enter a sentence please: helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello
Enter a sentence please: hello
Original String: helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello
Sub String: hello
Total Number Of Substring Appears: 7
Conclusion:
In this blog post, we have discussed all the possible ways in detail and shared 7 possible ways to find a substring in string Java. But let us know in the comment section if you are following any alternative way to find the matching substring.
Softwaretestingo Editorial Board
I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners.
All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.