Java StringBuilder delete() methodThe delete(int start, int end) method of Java StringBuilder class is used to delete the substring from specified start index to exclusive end index of this sequence. If the start and end index were same then no deletion takes place and this sequence remains unchanged. Syntax:Parameter:
Returns:The delete(int start, int end) method returns this object. Exception:StringIndexOutOfBoundsException - if start index is negative or any of index are greater than the length(), or the start index is greater than the end index. Compatibility Version:Java 1.5 and above Example 1Test it NowOutput: string1 = javatpoint After deleting = jaoint string2 = let us learn java After deleting = learn java Example 2Providing the negative (or less than 0) index in delete(int,int) method throws an exception. Test it NowOutput: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2 at java.lang.AbstractStringBuilder.delete(Unknown Source) at java.lang.StringBuilder.delete(Unknown Source) at snippet.StringBuilderDeleteExample2.main(StringBuilderDeleteExample2.java:6) string = javatpoint Example 3Providing the start index and end index as same value, no deletion take place and this sequence remains unchanged. Test it NowOutput: string = java code After deleting = java code Example 4Taking the string input from a user in string builder and using in delete(int,int) method. Test it NowOutput: enter your string value: string builder enter start index: 3 enter end index: 7 Count = strbuilder
Next TopicStringBuilder class
|