GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java StringBuilder replace() Method
Selected Reading
Java StringBuilder replace() MethodThe Java StringBuilder replace() method is used to replace the characters in a substring of a StringBuilder object with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence, if no such character exists. The replace() method accepts three parameters as an integer and string that holds the values of the start, end, and str. It throws an exception if the start index value is negative or greater than the string length. SyntaxFollowing is the syntax of the Java StringBuilder replace() method − public StringBuilder replace(int start, int end, String str) Parameters
Return ValueThis method returns this object. Example: Replacing a Substring with another StringIf the given start index value is positive, and less than the string length, the replace() method replaces the character in the specified string. In the following program, we are instantiating the StringBuilder class with the value Java Programming. Then, using the replace() method, we are trying to replace the character of the sub-string Language of this sequence at the startIndex 5, and endIndex 16. public class Replace {
public static void main(String[] args) {
//instantiate the StringBuilder class
StringBuilder sb = new StringBuilder("Java Programming");
System.out.println("The given String is: " + sb);
//initialize the startIndex, endIndex, and string value
int startIndex = 5;
int endIndex = 16;
String str = "Language";
System.out.println("The given startIndex and endIndex values are: " + startIndex + " and " + endIndex);
System.out.println("The given sub-string is: " + str);
//using the replace() method
System.out.println("After replace the string: " + sb.replace(startIndex, endIndex, str));
}
}
OutputOn executing the above program, it will produce the following result − The given String is: Java Programming The given startIndex and endIndex values are: 5 and 16 The given sub-string is: Language After replace the string: Java Language Example: Facing IndexOutOfBoundException while Replacing a Substring with another StringIf the given startIndex value is negative, the replace() method throws IndexOutOfBoundException. In the following example, we are creating an object of the StringBuilder class with the value TutorialsPoint. Using the replace() method, we are trying to replace the characters of the sub-string Point of this sequence at the startIndex -1 and endIndex 10. public class Replace {
public static void main(String[] args) {
try {
//create an object StringBuilder class
StringBuilder sb = new StringBuilder("TutorialsPoint");
System.out.println("The given String is: " + sb);
//initialize the startIndex, endIndex, and string value
int startIndex = -1;
int endIndex = 10;
String str = "India";
System.out.println("The given startIndex and endIndex values are: " + startIndex + " and " + endIndex);
System.out.println("The given sub-string is: " + str);
//using the replace() method
System.out.println("After replace the string: " + sb.replace(startIndex, endIndex, str));
} catch(IndexOutOfBoundsException e) {
e.printStackTrace();
System.out.println("Exception: " + e);
}
}
}
OutputFollowing is the output of the above program − The given String is: TutorialsPoint The given startIndex and endIndex values are: -1 and 10 The given sub-string is: India java.lang.StringIndexOutOfBoundsException: Range [-1, 10) out of bounds for length 14 at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55) at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210) at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112) at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349) at java.base/java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:987) at java.base/java.lang.StringBuilder.replace(StringBuilder.java:497) at Replace.main(Replace.java:16) Exception: java.lang.StringIndexOutOfBoundsException: Range [-1, 10) out of bounds for length 14 Example: Facing IndexOutOfBoundException while Replacing a Substring with another StringIf the given start index value is greater than the string length, this method throws the IndexOutOfBoundException. In this example, we are instantiating the StringBuilder class with the value hello. Using the replace() method, we are trying to replace the characters of the sub-string llo of this sequence at the startIndex 10 and endIndex 5. public class Replace {
public static void main(String[] args) {
try {
//instantiate the StringBuilder class
StringBuilder sb = new StringBuilder("hello");
System.out.println("The given String is: " + sb);
//initialize the startIndex, endIndex, and string value
int startIndex = 10;
int endIndex = 5;
String str = "world";
System.out.println("The given startIndex and endIndex values are: " + startIndex + " and " + endIndex);
System.out.println("The given sub-string is: " + str);
//using the replace() method
System.out.println("After replace the string: " + sb.replace(startIndex, endIndex, str));
} catch(IndexOutOfBoundsException e) {
e.printStackTrace();
System.out.println("Exception: " + e);
}
}
}
OutputAbove program produces the following output − The given String is: hello The given startIndex and endIndex values are: 10 and 5 The given sub-string is: world java.lang.StringIndexOutOfBoundsException: Range [10, 5) out of bounds for length 5 at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55) at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210) at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112) at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349) at java.base/java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:987) at java.base/java.lang.StringBuilder.replace(StringBuilder.java:497) at Replace.main(Replace.java:16) Exception: java.lang.StringIndexOutOfBoundsException: Range [10, 5) out of bounds for length 5 java_lang_stringbuilder.htm Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |