Site icon TestingDocs.com

Get Substring of Given String in Java

Overview

In this tutorial, we will learn to extract or get a substring of a given String in a Java program.

Sample Code

Sample Java program to show the use of the substring() method.

/*******************************************
 * Java String substring() method Example
 * Java Tutorials - www.TestingDocs.com
 *
 ********************************************/
public class JavaSubStringExample {

	public static void main(String args[]) {
		String str= new String("www.TestingDocs.com 
Software Testing Website ");
		System.out.println("Substring upto index 20:");
		System.out.println(str.substring(0,20));
		System.out.println("Substring from
 index 20 and ending at 45:");
		System.out.println(str.substring(20, 45));
	} 
}

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Exit mobile version