1:17 am - Wednesday January 25, 2017

Insert the string or word in a StringBuffer at the specified position

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

                                       In this Tutorial you will learn how to insert String Using StringBuffer Insert Method With Example program.

public class StringBufferInsertExample {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("Stu_Name  Stu_Age  Stu_Rank");
		sb.insert(8, "  Stu_Id");
		System.out.println(sb);
	}
}

OUTPUT:

Stu_Name Stu_Id Stu_Age Stu_Rank

Expalanation:

public StringBuffer insert(int offset, String str)

 

Inserts the string into this character sequence.

The characters of the String argument are inserted, in order, into this sequence at the indicated offset, moving up any characters originally above that position and increasing the length of this sequence by the length of the argument. If str is null, then the four characters “null” are inserted into this sequence.

The character at index k in the new character sequence is equal to:

  • the character at index k in the old character sequence, if k is less than offset
  • the character at index k-offset in the argument str, if k is not less than offset but is less than offset+str.length()
  • the character at index k-str.length() in the old character sequence, if k is not less than offset+str.length()

The offset argument must be greater than or equal to 0, and less than or equal to the length of this sequence.

Parameters:

offset – the offset.

str – a string.

Returns:

a reference to this object.

Throws:StringIndexOutOfBoundsException – if the offset is invalid.

BACK

Filed in: Java Examples

Comments are closed.