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";
str.subString(3);
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";
str.subString(0,3);
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.
Program:
public String subString(int startIndex): This method returns a string having specified index to the end of the string.
Ex: String str = "PLMDeveloper";
str.subString(3);
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";
str.subString(0,3);
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.
Program:
public class SubStringExample {
public static void main(String[] args) {
String str = "PLMDeveloper";
String str1=str.substring(3);
System.out.println("str1::::::::"+str1);
String str2=str.substring(0, 3);
System.out.println("str2::::::::"+str2);
}
}
0 Comments