jp
BAN USERimport java.util.Scanner;
public class SubString {
static String getSubString(String str, int startIndex, int endIndex) {
if (str == null) {
throw new NullPointerException();
}
if (endIndex > str.length() || startIndex < 0 || startIndex > endIndex) {
throw new IllegalArgumentException();
}
StringBuilder subString = new StringBuilder();
for (int i = startIndex; i < endIndex; i++) {
subString.append(str.charAt(i));
}
return subString.toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
System.out.print("Enter String : ");
str = sc.next();
int startIndex, endIndex;
System.out.print("Enter start index : ");
startIndex = sc.nextInt();
System.out.print("Enter end index : ");
endIndex = sc.nextInt();
System.out.println("Sub String : "
+ getSubString(str, startIndex, endIndex));
sc.close();
}
}
- jp June 16, 2015