What is a Substring in Java & How Does it Work | DataTrained

Chandrakishor Gupta Avatar

About

A contiguous series of characters contained within a string is known as a substring, Substring in Java. It can also be a subset or portion of another string. By way of illustration, “Substring” is a substring of “Substring in Java.” One of the most widely utilized languages among web developers worldwide is Java.

What is a Substring in Java?

What is a Substring in Java?

Substring in JAVA is a frequently used Java.lang function. Using the larger string as a starting point, a smaller string can be created. The original string remains unchanged since strings in Java are immutable, and the method returns a new string. 

Syntax:

string.substring(int startIndex, int endIndex)

Note: The startIndex is inclusive while the endIndex is exclusive in this instance, and the string is an object of the String class. Below, we’ll look into the use in more detail.

How Does the Method of Substring() Operate?

How Does the Method of Substring() Operate?

To understand how the substring() method works, let’s look at an example. Let’s say we want to extract the substring “World” from the text “Hello, World!” We can accomplish this by using the substring() method, as seen below: To learn about coding

String str = “Hello, World!”;

String substr = str.substring(7, 12);

A section of a string can be extracted using the Java substring(begIndex, endIndex) function. It gives back a brand-new string that contains all of the characters from the starting index (inclusive) all the way to the ending index (exclusive) of the original string.

String str = “Hello World”;

String substr = str.substring(6, 11);

System.out.println(substr); // Output: “World”

With parameters 6 and 11, the str string is passed to the substring() method in this example. Substring in JAVA, As a result, the new substring will begin at index 6 (the letter “W” from “World”) and terminate at index 11 (the final letter “d” from “World”). Assigning the resulting substring to the substrate variable and printing it to the console follows.

Methods to Get a Substring

Methods to Get a Substring

With one of the two variations, a substring in JAVA can be created from a specified string object:

String substring(): 

There are two variations of this method, which returns a new string that is a substring of the original string. The character at the supplied index serves as the start of the substring, which continues until the conclusion of this string. Endindex of the substring in java does not begin at 0 but rather at 1. Click here to know about data science course Edinburgh

Syntax : 

public String substring(int begIndex)

Parameters : 

begIndex : the begin index, inclusive.

Return Value : 

The specified substring.

Code

// Java code to demonstrate the

// working of substring(int begIndex)

public class Substr1 {

public static void main(String args[])

{

// Initializing String

String Str = new String(“Welcome to DataTrained”);

// using substring() to extract substring

// returns (whiteSpace)DataTrained

System.out.print(“The extracted substring is : “);

System.out.println(Str.substring(10));

}

}

Output

The extracted substring is :  DataTrained

String substring(begIndex, endIndex): 

There are two variations of this method, which returns a new string that is a substring of the original string. The substring in JAVA starts at the character at the given index and goes all the way to the end of this string, or up to endIndex – 1 if the second parameter is provided. 

Syntax : 

public String substring(int begIndex, int endIndex)

Parameters : 

beginIndex : the begin index, inclusive.

endIndex : the end index, exclusive.

Return Value : 

The specified substring.

Code

// Java code to demonstrate the

// working of substring(int begIndex, int endIndex)

public class Substr2 {

public static void main(String args[])

{

// Initializing String

String Str = new String(“Welcome to DataTrained”);

// using substring() to extract substring

// returns Data

System.out.print(“The extracted substring is : “);

System.out.println(Str.substring(10, 16));

}

}

Output: 

The extracted substring  is :  Data

Time Complexity: 

O(n),where n represents how long the initial string was. To return the substring, the substring() method requires constant time O(1).

Space Complexity: 

O(1),as the substring procedure doesn’t require any additional space.

Possible application: 

The prefix and suffix extraction are just two uses for the substring extraction. For instance, you might want to extract the Lastname from the name or just the denomination from a string that contains both the amount and the currency sign. The latter is detailed further down. Also read about investment banking course Edinburgh

Code

// Java code to demonstrate the

// application of substring()

public class Appli {

public static void main(String args[])

{

// Initializing String

String Str = new String(“Rs 1000”);

// Printing original string

System.out.print(“The original string is : “);

System.out.println(Str);

// using substring() to extract substring

// returns 1000

System.out.print(“The extracted substring is : “);

System.out.println(Str.substring(3));

}

}

Output : 

The original string  is : Rs 1000

The extracted substring  is : 1000

Example Program for Substring in Java (Exception)

Example Program for Substring in Java

Code

public class Substring {

  public static void main(String[] args) {

    String s = “Hello Trainer!”;

    /* Throws StringIndexOutOfBoundsException as endIndex is out of range of string s */

    System.out.println(s.str.substring(1, 30));

  }

}

Output:

StringIndexOutOfBoundsException

Conclusion

The Substring in Java method merely returns a portion of the original string, which is one of the important data types. When the substring method is used, a new string with an offset and count is produced. To learn about substrings, we have provided numerous examples using a variety of techniques. There is code and corresponding screenshots included.

Substring in JAVA, The “substring” function has various uses, including identifying all substrings of a string, locating a string’s prefixes and suffixes, determining whether a string is a palindrome and many more.

Frequently Asked Questions

How does substring () inside string works?

The character at the supplied index serves as the start of the substring, which continues until the conclusion of this string. beginIndex and endIndex are integers. The substring stretches from the character at the supplied beginIndex to the character at endIndex – 1. As a result, the substring has a length of (endIndex – beginIndex).

The split(String regex, int limit) method divides this string based on where the provided regular expression matches this text. Each substring of this string that is terminated by either the end of the text or another substring that meets the supplied expression is included in the array produced by this method.

Calling the Substring(Int32, Int32) method will allow you to take a substring that starts at a specific character position and terminates before the string’s end. The current instance’s value is not changed by this procedure. Instead, it produces a fresh string starting at the startIndex position of the previous one.

Java String substring() Method example.
the String class’s substring function (int beginIndex, int endIndex). A fresh string that is a part of the original string is given back. The substring in JAVA stretches from the character at index beginIndex to endIndex – 1.

Using substring() method

In order to insert a string, we will use substring in JAVA() function. This function is a part of Java’s String class. The substring() method only returns a portion of the string; the original string is not altered.

A character or substring will be swapped out for another using the Java string replace() function. The string_name syntax is used in the replace() method. replace(old_string, new_string), where new_string is the substring that will replace the one you want to replace.

Tagged in :

More Articles & Posts

UNLOCK THE PATH TO SUCCESS

We will help you achieve your goal. Just fill in your details, and we'll reach out to provide guidance and support.