
String subSequence() With Negative Example: In the below example we will try to execute by entering the negative value in the index position. There are two types of substring methods in Java string. In other words, the beginIndex starts from 0, whereas the endIndex starts from 1. ("substring equals subSequence ? " + (str.substring(4, 14).equals(str.subSequence(4, 14)))) We pass beginIndex and endIndex number position in the Java substring method where beginIndex is inclusive, and endIndex is exclusive. When you compare with equals(), its comparing value String substring (): This method has two variants and returns a new string that is a substring of this string. ("substring = subSequence ? " + (str.substring(4, 14) = str.subSequence(4, 14))) When you are compare with = its comparing Hashcode which is different ("website name: " + str.subSequence(4, 20)) ("First 4 char String: " + str.subSequence(0, 4)) String str = "("Last 4 char String: " + str.subSequence(str.length() - 4, str.length())) A more complex version of substring() takes both start and end index numbers, and returns a string of the chars between start and one before end. This method returns CharSequence from a string.
#Java substring start and end index equals 0 how to#
Return this.substring(beginIndex, endIndex) Įxample Of Java String subSequence(): The code snippet below demonstrates how to use the subSequence() method. o lastIndexOf(int, int): Returns the index within this String of the. And the code snippet is looking something like the below: public CharSequence subSequence(int beginIndex, int endIndex) Returns the index within this String of the last occurrence of the specified character.

The extracted portion starts with the character at the index beginIndex till the end of the string.Tips: When we are invoking the subSequence() method it internally invokes the substring() method.

Otherwise the substring() method will simply throws IndexOutOfBoundsException. The beginIndex should be greater than zero and less than the length of the string.Īlso, endIndex should be greater than or equal to beginIndex and less than the length of the given string. When substring() Throws IndexOutOfBoundsException? Please bear in mind that both methods are susceptible to throw IndexOutOfBoundsException. Public String substring(int startIndex, int endIndex): this variant accepts two parametersīeginIndex: describes the starting index, it’s inclusive for both variantsĮndIndex: denotes the last index and it’s exclusive Public String substring(int startIndex): this version accepts only one parameter String.substring() Syntax Variants in Javaīasically, there are two overloaded variants of the substring() method: So, let’s dig deep and take a close look at it. Now that we know what a substring is, let’s see how to create and manipulate it.įortunately, the String class provides a handy method called substring() especially to get a substring. In short, every subset or portion of an original string is called a substring.įor example, “devwithus” is composed of the following substrings: “dev”, “with”, “us”… What is a substring?Ī substring, as the name implies, is a contiguous sequence of characters within a particular string. So, this is where the substring() method comes to the rescue.

We need just to manipulate some of them to solve one problem or another. However, sometimes we are not interested in all characters. String in Java is a class that represents a sequence of characters. In this article, we are going to shed light on Java String substring() method.

Important Key Points about the substring() Method.How substring() is Implemented Internally in Java?.substring(int beginIndex, int endIndex) Example.When substring() Throws IndexOutOfBoundsException?.String.substring() Syntax Variants in Java.
