package org.kodejava.lang;
public class StringBufferInsert {
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer("kodeava");
System.out.println("Text before = " + buffer);
// |k|o|d|e|a|v|a|....
// 0|1|2|3|4|5|6|7|...
//
// From the above sequence you can see that the index of the
// string is started from 0, so when we insert a string in
// the fourth offset it means it will be inserted after the
// "e" letter. There are other overload version of this
// method that can be used to insert other type of data such
// as char, int, long, float, double, Object, etc.
buffer.insert(4, "j");
System.out.println("After first insert = " + buffer);
// Here we insert a string to the StringBuffer at index 8
buffer.insert(8, " examples");
System.out.println("Final result = " + buffer);
}
}
The program will print the following output:
Text before = kodeava
After first insert = kodejava
Final result = kodejava examples
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024