HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / String / Java String substring()

Java String substring()

The String substring() method returns a new string that is a substring of the given string.

substring() uses the start and optionally the end indices, passed as method arguments, to determine the substring position within the original string.

String substring(int beginIndex)
String substring​(int beginIndex, int endIndex)
String text = "Lorem Ipsum is simply dummy text";

String substring = text.substring(5);
String substring = text.substring(3, 10);

1. String substring() Method

The substring() method in the java.lang.String class is an overloaded method:

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

Method Parameters

The substring() method arguments are:

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

Return Value

It returns the substring from the given string.

2. Find substring with the begin index

Supply only begin index when we need to find a substring starting from the given index position to the end of the string.

String substring(int beginIndex)

// Where 0 <= beginIndex <= Length of the String
  • It returns the substring starting from the specified index till the end of the given string.
  • indices range with 0 to N where N is length of string.
  • The begin index location is inclusive i.e. the result substring will contain the character at index position 'beginIndex'.
  • It throws IndexOutOfBoundsException if the beginIndex is less than zero or greater than the length of the given string (N).
  • If we pass the index value N (string lenth), an empty string is returned.

Example: Find substring with beginning index

In the given example, character 'e' is in the index position 3. The substring() includes the begin index in the output so the returned substring output starts from e till the end of the string.

String text = "Lorem Ipsum is simply dummy text";
String substring = text.substring(3);
System.out.println(substring);

Program output.

em Ipsum is simply dummy text

3. Find substring with the begin index and the end indices

Supply both indices (begin index and end index) when we need to find a substring starting from the given index position to the given end index position.

String substring(int beginIndex, int endIndex)

// Where: 
// 0 <= beginIndex <= Length of the String
// beginIndex <= endIndex <= N
  • It returns the substring starting from the specified index till the end index position.
  • indices range with 0 to N where N is length of string.
  • The begin index location is inclusive and end index position is exclusive. i.e. the result substring will contain the character at index position 'beginIndex' but the will not contain the character at index position.
  • It throws IndexOutOfBoundsException if the beginIndex is less than zero or greater than the length of the given string (N). Also, endIndex should be greater than or equal to beginIndex and less than the length of the given string (N).
  • If we pass the same index in both parameters then the empty string is returned.

Example: Find substring with beginning index and the end index

In the given example, character 'e' is in the begin index position 3 and 'm' is in the end index position 10.

The character in the begin index is included, while the character at the end index is excluded.

The length of the substring is equal to the difference between the end index and begin index positions.

String text = "Lorem Ipsum is simply dummy text";
String substring = text.substring(3, 10);
		
System.out.println(substring);
System.out.println(substring.length());

Program output.

em Ipsu
7

4. Blank spaces are also counted in substring

While calculating the index locations, please be aware of blank spaces. A blank space is also taken as one character in this string.

In the given example, we are getting the substring from index locations 3 to 8. At location 3, we have a blank space. This blank will also be part of the substring returned from the method.

String str = "The blog name is howtodoinjava.com";
        
System.out.println(str.substring(3, 8));    //<space>blog

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

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

Feedback, Discussion and Comments

  1. Shikha

    September 28, 2019

    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”.

Comments are closed on this article!

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

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

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 © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces