×

Check the presence of Substring in a String in java

In java, the string can be treated as class and datatype. The string contains words and numbers but should be in double-quotes.

Example: ” Omsairam”

Substring

The part of the string is called the substring. Java String class contains an inbuilt method substring(), which is used for accessing a particular part of the string.

Example: Sachin Tendulkar. In this example, Tendulkar is the substring.

A string is defined as a group/collection of words. To access a particular word, some methods should be followed. In Java, String is a class and also a Datatype. To get a word from a string, there are two methods, which are as follows:

  1. IndexOf( )
  2. Contains( )

These two methods are from the String class in java.

IndexOf() method

This method is used to find the index of the string we need to find. It finds the index of that in the main String. The result is the index of the substring, an integer that returns -1 if the word or substring is not present in the string.

Consider an example for index() of method :

//Program

public class Word{
public static void main(String[] args){
String str = "Java is Object Oriented Programming Language";
System.out.println(str);
// check whether the word present in it or not
String word = "is Object";
int i = str.indexOf(word);
if(i>0)
System.out.println(str.substring(i, i+word.length()));
else 
System.out.println("string not found");
}
}

Output

Check the presence of a Substring in a String in Java

2.Contains( ) method

This method is also used to check whether the string contains the given the word.

The result of this method is a Boolean value, true or false. The result will be true if

It is found. Else it returns false.

Example

public class Word {
public static void main(String[] args){
String str = "Java is a object oriented programming language";
System.out.println(str);
// check the in substring in a string
String ss = "programming";
boolean val = str.contains(ss);
if(val)
System.out.println("String found: "+ss);
else 
System.out.println("String Not found");
}
}

Output

Check the presence of a Substring in a String in Java

Related Topics

Advanced Java Viva Questions

One of the more difficult languages available now is Java. Currently, 10 thousand developers worldwide use the programming language, which is rising daily. So, if you're a Java developer, an aspiring...

9 minutes read.

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked...

3 minutes read.

Addition Program in Java

Addition Program in Java We can perform the addition (sum) of two or more numbers by using the arithmetic operator (+). To write an addition program in Java, one must understand...

3 minutes read.

Set Value to Enum in Java

In this article, you will be acknowledged about Enum in java. Most importantly you will learn how to set value to Enum or how to practically customize a value to...

3 minutes read.

How to Calculate Time Difference Between Two Dates in Java?

Date is being used extensively in Java to calculate date differences. While constructing an application, the date of joining an organisation, admission date, appointment date, and others might be included....

4 minutes read.

Java Return Keyword

The return keyword in Java is used to end a method's execution. the caller receives the return, followed by the appropriate value. The return type of the method, such as...

3 minutes read.

How to find the length of an Array in Java

Arrays: An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be...

3 minutes read.

Bad Operand types for Binary Operator Java

We will discuss how to handle issues in bad operand types for binary operator &, bad operand types for binary operator &&, bad operand types for binary operator ==, and...

4 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

Java Try-Catch Block

Java Try Block The handling of the exceptions in a block of code is done with the help of java try block. It throws the code that is enclosed in a...

3 minutes read.

Recursion Program in Java

The recursion program in Java demonstrates the usage of recursion. The process by which a function/ method calls itself, again and again, is called recursion. Each recursive call is pushed...

10 minutes read.

Why are generics used in Java

Java has a feature called generics that allows you to make a class, interface, and function accepting any (reference) type as a parameter. In other words, it is the idea...

4 minutes read.

Singleton Design Pattern in Java

In the singleton pattern, a class that only has one instance and offers a universal point of access is taken into consideration. It can also be defined in another way, a...

4 minutes read.

All the important string methods in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

Convert list to array Java

One of the popular collection interfaces for storing an ordered collection is the List. The List interface may contain repeating groups and preserves the insertion order of entries. This article will...

4 minutes read.

How to convert double to String in Java

How to Convert double to String in Java It is used when we want to convert double primitive to String type. There are two methods to convert double to String. Using String.valueOf()...

2 minutes read.

nth Prime Number in Java

A number is considered prime if only divisible by one and itself. Prime numbers include, for example, 2, 3, 5, 7, 11, etc. In looking at it another way, a...

5 minutes read.