Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java String Operations: Checking if One String Contains Another, Study notes of Java Programming

In this java tutorial, we explore how to use the string.contains() method to determine if one string includes another string in java. We provide code examples and explanations.

What you will learn

  • What is the output of the Java program when checking if 'apple' contains 'pp'?
  • How do you check if a string contains another string in Java?
  • What is the Java method for checking if a string includes another string?

Typology: Study notes

2021/2022

Uploaded on 08/05/2022

nguyen_99
nguyen_99 🇻🇳

4.2

(80)

1K documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java – Check if a String contains Another String
To check if a String str1 contains another string str2 in Java, use String.contains() method. Call
contains() method on the string str1 and pass the other string str2 as argument. If str1 contains the
string str2 , then contains() method returns true.
Java Program
Output
If str1 does not contain the string str2 , then contains() method returns false.
Java Program
Output
How to Check if a String contains Another
String in Java?
public class Example {
public static void main(String[] args) {
String str1 = "apple";
String str2 = "pp";
boolean result = str1.contains(str2);
System.out.println("Does str1 contains str2? " + result);
}
}
Does str1 contains str2? true
public class Example {
public static void main(String[] args) {
String str1 = "apple";
String str2 = "kk";
boolean result = str1.contains(str2);
System.out.println("Does str1 contains str2? " + result);
}
}
Does str1 contains str2? false
pf2

Partial preview of the text

Download Java String Operations: Checking if One String Contains Another and more Study notes Java Programming in PDF only on Docsity!

Java – Check if a String contains Another String

To check if a String str1 contains another string str2 in Java, use String.contains() method. Call

contains() method on the string str1 and pass the other string str2 as argument. If str1 contains the

string str2 , then contains() method returns true.

Java Program Output

If str1 does not contain the string str2 , then contains() method returns false.

Java Program Output

How to Check if a String contains Another

String in Java?

public class Example { public static void main(String[] args) { String str1 = "apple"; String str2 = "pp"; boolean result = str1.contains(str2); System.out.println("Does str1 contains str2? " + result); } } Does str1 contains str2? true public class Example { public static void main(String[] args) { String str1 = "apple"; String str2 = "kk"; boolean result = str1.contains(str2); System.out.println("Does str1 contains str2? " + result); } } Does str1 contains str2? false

Conclusion In this Java Tutorial, we learned how to check if a string contains another string in Java. ✦ Java Tutorial Java String Operations ✦ Java String ✦ Java String Operations ✦ Java - Print String to Console ✦ Java - Read String from Console ✦ Java - Concatente two Strings ✦ Java - Check if Strings are Equal ✦ Java - Find Index of First Occurrence of Substring ✦ Java - Find Index of Nth Occurrence of Substring ✦ Java - Replace First Occurrence of Substring ✦ Java - Replace All Occurrences of Substring ✦ Java - Reverse a String ✦ Java - Split String ✦ Java - Trim String