Write a program to get a substring of a given string between two specified positions


This code prints out a specified portion of a given string. It takes the original string "Tutor Joe's Computer Education" and then uses the substring method to extract a substring starting from position 11 and ending at position 20 (exclusive), which corresponds to the substring "Computer". Here's a breakdown of the code:

  • String str = "Tutor Joe's Computer Education";: defines a string variable str and assigns it the value "Tutor Joe's Computer Education".
  • String new_str = str.substring(11, 20); : defines a new string variable new_str and assigns it the value of a substring of str starting from position 11 and ending at position 20 (exclusive).
  • System.out.println("Given String : " + str);: prints out the original string.
  • System.out.println("SubString of a given String : " +new_str); : prints out the extracted substring.

Source Code

public class Specified_Position
{
	public static void main(String[] args)
	{
		String str = "Tutor Joe's Computer Education";
		String new_str = str.substring(11, 20);
		System.out.println("Given String : " + str);
		System.out.println("SubString of a given String : " +new_str);
	}
}

Output

Given String : Tutor Joe's Computer Education
SubString of a given String :  Computer

Example Programs