How do I check if a string contains a substring in Java

0 votes
Hi Team,

I have two Strings, str1 and str2. How do I check if str2 is contained within str1?
Dec 30, 2020 in Java by akhtar
• 38,170 points
120 views

1 answer to this question.

0 votes

Hi@akhtar,

The first and foremost way to check for the presence of a substring is the .contains() method. It's provided by the String class itself and is very efficient. The method accepts a CharSequence and returns true if the sequence is present in the String we call the method on.

String string = "Java";
String substring = "va";
System.out.println(string.contains(substring));
answered Dec 30, 2020 by MD
• 95,060 points

Related Questions In Java

0 votes
4 answers

How do I check if a file exists in Java?

To test to see if a file ...READ MORE

answered Dec 28, 2020 in Java by Rajiv
• 8,880 points
59 views
0 votes
1 answer

How do I convert a String to an int in Java?

Hello @kartik, We can use the parseInt(String str) method of ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,560 points
136 views
0 votes
1 answer

How do I determine whether an array contains a particular value in Java?

Arrays.asList(yourArray).contains(yourValue) Warning: this doesn't work for arrays of ...READ MORE

answered Dec 21, 2020 in Java by Gitika
• 65,870 points
33 views
0 votes
3 answers

How to check if a String is numeric in Java?

Check if a string is numeric public class ...READ MORE

answered Dec 28, 2020 in Java by Carlos
87 views
0 votes
2 answers

Store String inside a File using Java

We can use Apache Commons IO. It ...READ MORE

answered Jul 20, 2018 in Java by Sushmita
• 6,890 points
157 views
0 votes
3 answers

Adding text to a file using Java

try { final Path ...READ MORE

answered Sep 6, 2018 in Java by Sushmita
• 6,890 points
242 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
275 views
0 votes
1 answer

How to split Strings by space in Java ?

You can use split() method. str = "Hello ...READ MORE

answered May 16, 2018 in Java by sharth
• 3,350 points
291 views
0 votes
1 answer

How to Install Java in Windows system?

Hi@akhtar, You can follow the below-given steps to install ...READ MORE

answered Jul 6, 2020 in Java by MD
• 95,060 points
1,079 views
0 votes
1 answer

How to remove the last character from a string?

Hi@akhtar, You can use the StringBuffer class. It provides ...READ MORE

answered Dec 30, 2020 in Java by MD
• 95,060 points
87 views