
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What is a sub string in Java?
The String class of the java.lang package represents set of characters. All string literals in Java programs, such as "abc", are implemented as instances of this class. A string index is an integer representing the position of each character in the string starting from zero.
A substring is a part/segment of a string. You can identify a substring of a string using the substring() method of the String class. This method have two variants −
substring(int beginIndex)
This method accepts an integer value representing an index in the current string and returns the substring starting from the given index to the end of the string.
Example
import java.util.Scanner; public class SubStringExample { public static void main(String[] args) { System.out.println("Enter a string: "); Scanner sc = new Scanner(System.in); String str = sc.nextLine(); System.out.println("Enter the index of the substring: "); int index = sc.nextInt(); String res = str.substring(index); System.out.println("substring = " + res); } }
Output
Enter a string: Welcome to Tutorialspoint Enter the index of the string: 11 substring = Tutorialspoint
substring(int beginIndex, int endstring)
This method accepts two integer values representing the index values of the current string and returns the substring between the given index values.
Example
import java.util.Scanner; public class SubStringExample { public static void main(String[] args) { System.out.println("Enter a string: "); Scanner sc = new Scanner(System.in); String str = sc.nextLine(); System.out.println("Enter the start index of the substring: "); int start = sc.nextInt(); System.out.println("Enter the end index of the substring: "); int end = sc.nextInt(); String res = str.substring(start, end); System.out.println("substring = " + res); } }
Output
Enter a string: hello how are you welcome to Tutorialspoint Enter the start index of the substring: 10 Enter the end index of the substring: 20 substring = are you we
- Related Articles
- Method to check if a String contains a sub string ignoring case in Java
- How to replace a sub-string with the reverse of that sub-string in R?
- Check if a string contains a sub-string in C++
- What is a Sub-atomic Particle?
- How to check if a string contains a specific sub string?
- Maximum even length sub-string that is permutation of a palindrome in C++
- Count all Palindrome Sub-Strings in a String in C++
- Check if a string contains a palindromic sub-string of even length in Python
- Check if a string contains a palindromic sub-string of even length in C++
- What is the difference between a String object and a String literal in Java?
- What is string constant pool in Java?
- What is Java String Class?
- What is Java String literal?
- Find sub-string with given power in C++
- What is special about string class in Java?
