AppDividend
Latest Code Tutorials

Java String substring() Function Example

0

A substring of a string is defined as a sequence of characters in the string starting at some index and going continuously (without skipping any index or changing the order of characters) up to some higher index. The right index of the substring must be greater than or equal to the left index.

Java String substring()

The java.lang.String.substring() method returns the substring of the string starting from a specified index and going till the end of the string or up to a specified end index(excluding that index). See the following figure.

 

Java String substring() Function Example

Java substring() method always returns a new string, and the original string remains unchanged because String is immutable in Java.

Internal Implementation

public String substring(int beginIndex) {  
  if (beginIndex < 0) {  
      throw new StringIndexOutOfBoundsException(beginIndex);  
  }  
  int subLen = value.length - beginIndex;  
  if (subLen < 0) {  
      throw new StringIndexOutOfBoundsException(subLen);  
  }  
  return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);  
}

Java String substring(int beginIndex) example

Syntax: public String substring(int beginIndex)

Parameters:

int beginIndex The index from which the substring is to begin

Returns: The substring beginning at the specified index and going till the end of the string.

Throws: Throws StringIndexOutOfBoundsException if the parameter is negative or exceeds the length of the string.

Example1.java: Returning substring till the end of the string.

public class Example1 {
  public static void main(String[] args) {
    String s1 = new String("Consider this example.");
    System.out.println(s1.substring(9));
  }
}

Output

 

Java String substring()

Example1a.java: On passing a negative beginning index.

public class Example1a {
  public static void main(String[] args) {
    String s1 = new String("Consider this example.");
    System.out.println(s1.substring(-1));
  }
}

Output

 

Substring till the end of the string

Example1b.java: On passing a beginning index, which exceeds the length of the string.

public class Example1b {
  public static void main(String[] args) {
    String s1 = new String("example");
    System.out.println(s1.substring(8));
  }
}

Output

 

Example1b

Java String substring(int beginIndex, int endIndex) example

Syntax: public String substring(int beginIndex, int endIndex)

Parameters:

int beginIndex – The index from which the substring is to begin

int endIndex – The substring ends at endIndex – 1.

Returns: The substring beginning at the specified index and going till the index before the end index.

Throws: Throws StringIndexOutOfBoundsException if the parameter is negative or exceeds the length of the string.

Example2.java: Returning substring starting from beginIndex up to endIndex-1.

public class Example2 {
  public static void main(String[] args) {
    String s1 = new String("Consider this example.");
    System.out.println(s1.substring(9, 13));
  }
}

Output

Java String example

 

Example3.java: On passing a negative index.

public class Example3 {
  public static void main(String[] args) {
    String s1 = new String("Consider this example.");
    System.out.println(s1.substring(9, -1));
  }
}

Output

 

negative index

Example4.java: On end index exceeding the length of a string.

public class Example4 {
  public static void main(String[] args) {
    String s1 = new String("Consider.");
    System.out.println(s1.substring(9, 10));
  }
}

Output

 

On end index exceeding the length of a string

Conclusion

The Java String substring() method returns a new string that is a substring of this string. The substring() method is overloaded method and comes in two variants:

String substring(int beginIndex)
String substring​(int beginIndex, int endIndex)

Where method arguments are:

beginIndex – the beginning index, inclusive.
endIndex – the ending index, exclusive.

So, if you want to extract part of the string in Java, then you can use the Java substring() function.

Related Posts

Java String isEmpty()

Java String lastIndexOf()

Java String startsWith()

Java String split()

Java String join()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.