Java StringBuilder setCharAt() methodThe setCharAt(int index, char ch) method of Java StringBuilder class is used to set the specified character at the given index. The specified character altered this sequence by replacing the old character at the given index. Syntax:Parameter:
Returns:NA Exception:IndexOutOfBoundsException - if an index is negative or greater than or equal to length() of this sequence. Compatibility Version:Java 1.5 and above Example 1Test it NowOutput: string = abc character at index 1 = b new string = axc character at index 1 = x Example 2Test it NowOutput: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3 at java.lang.AbstractStringBuilder.setCharAt(Unknown Source) at java.lang.StringBuilder.setCharAt(Unknown Source) at snippet.StringBuilderSetCharAtExample2.main(StringBuilderSetCharAtExample2.java:6) string = abc Example 3Test it NowOutput: enter your string value: Java enter index value: 0 enter character value: j new string = java
Next TopicStringBuilder class
|