Java StringBuffer example to replace character at specific index using setCharAt


public void setCharAt(int index,char ch)
The character at the specified index is set to ch. This sequence is altered to represent a new character sequence that is identical to the old character sequence, except that it contains the character ch at position index.
The index argument must be greater than or equal to 0, and less than the length of this sequence.

Program:
package com.candidjava;

public class StringBufferSetCharAt 
{
	public static void main(String args[]) 
	{
		StringBuffer s=new StringBuffer("Machismo");
		s.setCharAt(4,'o');
		System.out.println("The newly characterised String is "+s);
	}
}


Output:
      The newly characterised String is Machosmo

Description:
      The original character 'i' at index 4 is being replaced by character 'o'.

Parameters:
index - the index of the character to modify.
ch - the new character.

Throws:
IndexOutOfBoundsException - if index is negative or greater than or equal to length().



Related Post

Comments


©candidjava.com