Table of Contents
String’s substring method is used to fetch part of a String.It returns a new String which is substring of given String. substring is nothing but subset of given String.
It returns a new String which is substring of given String from specified startIndex(Inclusive)
It returns a new String which is substring of given String from specified startIndex(Inclusive) to endIndex(Exclusive)
When you run above program, you will get below output:
There are two overloaded methods for substring.
Method Signature:
1 2 3 |
public String substring(int startIndex) |
1 2 3 |
public String substring(int startIndex, int endIndex) |
String’s index always starts with 0. In case of substring method, startIndex is always inclusive and endIndex is exclusive
String substring Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.java2blog; public class StringSubStringExample { public static void main(String[] args) { String str1= "java2blog.com"; // using beginIndex System.out.println(str1.substring(5)); // using beginIndex and endIndex System.out.println(str1.substring(5,9)); } } |
1 2 3 4 |
blog.com blog |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.