How to extract specific data from String Object using Sub String method in Java

By using subString() method in java we can extract specific data from string object in java. There are two types of subString methods in java.

public String subString(int startIndex): This method returns a string having specified index to the end of the string.

Ex: String str = "PLMDeveloper";
Result: Developer

Note: Here startIndex letter Included in the string

public String subString(int startIndex, int endIndex): Here startindex is the index of starting point of the string and endIndex is the end point of the string.

Ex: String str = "PLMDeveloper";
Result: PLM

Note: Here endIndex letter Excluded from the string

These methods throws IndexOutOfBoundException If startIndex is less than zero or endIndex is greater than the length of the index.


public class SubStringExample {

 public static void main(String[] args) {
  String str = "PLMDeveloper";

  String str1=str.substring(3);
  String str2=str.substring(0, 3);

Post a Comment