Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Java String contains Method

by suresh

The Java contains method is one of the Java String Methods that are available. This Java string contains function checks whether the string contains the user-specified character sequence (sequence of characters) or not. Based on the result, the string contains method will return Boolean True or False.

In this article, we will show how to write a string contains in Java with multiple examples. The basic syntax of the string.contains in Java Programming language is as shown below.

Java contains syntax

The following Java string contains function accepts the Character Sequence as the argument and check whether the string contains the user given sequence of characters or not.

public boolean contains(CharSequence seq); // It will return boolean True or False 

//In order to use in program
String_Object.contains(CharSequence seq)

Java String contains Method Example

In this Java program, We are going to use the Java string contains method to check whether the string contains the given characters (in sequence) or not.

// Java String.contains example
package StringFunctions;

public class ContainsMethod {
 public static void main(String[] args) {
 String str = "Tutorials On Java Programming";
 String str1 = "We are abc working at abc company";
 
 boolean a = str.contains("ori");
 boolean b = str.contains("On Java");
 boolean c = str1.contains("working");
 boolean d = str1.contains("abc");
 boolean e = str1.contains("xyz");
 
 System.out.println("Does the String str contains ori? = " + a);
 System.out.println("Does the String str contains On Java? = " + b);
 System.out.println("Does the String str1 contains working? = " + c);
 System.out.println("Does the String str1 contains abc? = " + d);
 System.out.println("Does the String str1 contains xyz? = " + e);
 }
}

OUTPUT

Java String contains Method 1

ANALYSIS

Within this String contains example, we declared two String variables Str, Str1, and assigned corresponding values using the following statement.

String str = "Tutorials On Java Programming";
String str1 = "We are abc working at abc company";

The following statement calls public boolean contains (CharSequence seq) method to check whether the string str contains the character sequence “ori” or not. If it is TRUE, it will return TRUE, otherwise False.

boolean a = str.contains("ori");

The following String contains statement will check whether the string str contains the char sequence On Java or not. If it is TRUE, it will return TRUE, otherwise False.

boolean b = str.contains("On Java");

Let us try the wrong value. The following statement will call the boolean contains (CharSequence seq) function to check whether the string str1 contains the character sequence xyz or not. We all know that it is False

boolean e = str1.contains("xyz");

Following Java System.out.println statements will print the output

System.out.println("Does the String str contains ori? = " + a);
System.out.println("Does the String str contains On Java? = " + b);
System.out.println("Does the String str1 contains working? = " + c);
System.out.println("Does the String str1 contains abc? = " + d);
System.out.println("Does the String str1 contains xyz? = " + e);

Java Contains Example

In this Java string contains program, we are going to ask the user to insert any word. Based on the string value entered by the user, the string contains method will display the message.

package StringFunctions;

import java.util.Scanner;

public class ContainsMethodex {
 private static Scanner sc;
 public static void main(String[] args) {
 sc = new Scanner(System.in);
 
 System.out.println("Please Enter any word: ");
 String str = sc.nextLine();
 
 if (str.contains("gate")) {
 System.out.println("Hey!! Welcome to Tutorial Gateway");
 }
 else {
 System.out.println("Please type any word related to gate");
 }
 }
}

OUTPUT

Java String contains Method 2

Let us enter a different word

Java String contains Method 3

ANALYSIS

Within this contains String Method example, the following statement will ask the user to enter any word. Then we are going to assign the user entered value to string variable str.

System.out.println("Please Enter any word: ");
String str = sc.nextLine();

Next, we used the If Else Statement to check where the user entered string is contains the character sequence “gate” or not.

  • If the condition is True, the System.out.println(“Hey!! Welcome to Tutorial Gateway”); statement will print
  • Else System.out.println(“Please type any word related to gate”); statement printed.

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy