public
String substring(int beginIndex, int endIndex)
Returns
a string that is a substring of this string. The substring begins at
the specified beginIndex and extends to the character at index
endIndex - 1. Thus the length of the substring is
endIndex-beginIndex.
class StringSubString1{ public static void main(String args[]){ String str = "Self Learning Java"; String str1 = str.substring(0, 5); String str2 = str.substring(5, 14); String str3 = str.substring(14, 18); System.out.println("str = " + str); System.out.println("str1 = " + str1); System.out.println("str2 = " + str2); System.out.println("str3 = " + str3); } }
Output
str = Self Learning Java str1 = Self str2 = Learning str3 = Java
1. Throws
IndexOutOfBoundsException, if the beginIndex is negative, or
endIndex is larger than the length of this String object, or
beginIndex is larger than endIndex.
class StringSubString1IndexOut{ public static void main(String args[]){ String str = "Self Learning Java"; String str1 = str.substring(6, 5); System.out.println("str = " + str); System.out.println("str1 = " + str1); } }
Output
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1954) at StringSubString1IndexOut.main(StringSubString1IndexOut.java:4)
No comments:
Post a Comment