HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Java String substring() method example

By Lokesh Gupta | Filed Under: Java String class

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

  1. String substring(int beginIndex)
  2. String substring​(int beginIndex, int endIndex)

Where method arguments are:

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

1. Java String substring(int beginIndex) example

It returns a string that is a substring of the string. The substring begins with the character at the specified 'beginIndex' to the end of string.

  1. Please note that index starts from ‘0’ which refer to first character of String.
  2. Method will throw StringIndexOutOfBoundsException error if argument index is not a valid index. A valid index is always greater than or equal to zero; or less than or equal to the length of string.

0 >= Valid index <= length of string

Java program to get substring of a string from given index.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";

        System.out.println(blogName.substring(3));	//todoinjava.com
        
        System.out.println("hello world".substring(6));	//world
    }
}

Program output.

todoinjava.com
world

2. Java String substring(int beginIndex, int endIndex) example

It returns a string that is a substring of the string. The substring begins with the character at the specified 'beginIndex' (included) to the 'endIndex' (excluded) position.

Java program to get substring of a string from given start index to end index.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";

        System.out.println(blogName.substring(14, blogName.length()));	//com
        	
        System.out.println("hello world".substring(6,9));	//wor
       
        System.out.println("0123456789".substring(3, 7));	//3456
    }
}

Program output.

com
wor
3456

In last statement, example makes more sense as each character represent it’s index position in the string. ‘0’ at 0th index, ‘1’ at first index and so on till ‘9’ at ninth position.

When we find a substring from index 3 to index 7, we get string ‘3456’. Begin index is included and end index ‘7’ got excluded.

Happy Learning !!

References:

Java String Doc

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

1
Leave a Reply

This comment form is under antispam protection
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
Shikha

Hey. Nice article. A small suggestion : as mentioned in the article ” The substring begins with the character at the specified ‘beginIndex’ to the ‘endIndex’ position.” You should also clarify that the ‘endIndex’ is excluded. As in, it is a [) behavior.

eg: in case of “hello world”.substring(6,9) “wor” is returned, and not “worl”.

Vote Up0Vote Down  Reply
2 months ago

Search Tutorials

String methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

String examples

  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • String – Alignment
  • String – Immutable
  • String – StringJoiner
  • Java – Split string
  • String – Escape HTML
  • String – Unescape HTML
  • String – Convert to title case
  • String – Find duplicate words
  • String – Left pad a string
  • String – Right pad a string
  • String – Reverse recursively
  • String – Leading whitespaces
  • String – Trailing whitespaces
  • String – Remove whitespaces
  • String – Reverse words
  • String – Find duplicate characters
  • String – Check empty string
  • String – Get first 4 characters
  • String – Get last 4 characters
  • String – (123) 456-6789 pattern
  • String – Interview Questions

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz