Skip to Content

Java StringBuffer insert method Example

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  1. /*
  2.    Java StringBuffer insert method Example
  3.    This example shows how a value can be inserted in to StringBuffer object.
  4. */
  5.  
  6. public class JavaStringBufferInsertExample {
  7.  
  8.   public static void main(String[] args) {
  9.     /*
  10.      Java StringBuffer class provides following methods to insert various
  11.      primitive values and objects to StringBuffer object at specified offset.
  12.     */
  13.    
  14.     /*
  15.      StringBuffer insert(int offset, boolean b) method inserts
  16.      boolean to StringBuffer object at specified offset
  17.     */
  18.     boolean b = true;
  19.     StringBuffer sb1 =  new StringBuffer("Hello  World");
  20.     sb1.insert(6,b);
  21.     System.out.println(sb1);
  22.  
  23.     /*
  24.      StringBuffer insert(int offset, char c) method inserts
  25.      character to StringBuffer object at specified offset
  26.     */
  27.     char c = 'Y';
  28.     StringBuffer sb2 =  new StringBuffer("Hello  World");
  29.     sb2.insert(6,c);
  30.     System.out.println(sb2);
  31.  
  32.  
  33.     /*
  34.      StringBuffer insert(int offset, char[] c1) method inserts
  35.      character array to StringBuffer object at specified offset
  36.     */
  37.     char[] c1 = new char[] {'Y','e','s'};
  38.     StringBuffer sb3 =  new StringBuffer("Hello  World");
  39.     sb3.insert(6,c1);
  40.     System.out.println(sb3);
  41.  
  42.  
  43.     /*
  44.      StringBuffer insert(int offset, double d) method inserts
  45.      double to StringBuffer object at specified offset
  46.     */
  47.     double d = 1.0;
  48.     StringBuffer sb4 =  new StringBuffer("Hello  World");
  49.     sb4.insert(6,d);
  50.     System.out.println(sb4);
  51.  
  52.  
  53.     /*
  54.      StringBuffer insert(int offset, float f) method inserts
  55.      float to StringBuffer object at specified offset
  56.     */
  57.     float f = 2.0f;
  58.     StringBuffer sb5 =  new StringBuffer("Hello  World");
  59.     sb5.insert(6,f);
  60.     System.out.println(sb5);
  61.  
  62.  
  63.     /*
  64.      StringBuffer insert(int offset, int i) method inserts
  65.      integer to StringBuffer object at specified offset
  66.     */
  67.     int i = 5;
  68.     StringBuffer sb6 =  new StringBuffer("Hello  World");
  69.     sb6.insert(6,i);
  70.     System.out.println(sb6);
  71.  
  72.  
  73.     /*
  74.      StringBuffer insert(int offset, long l) method inserts
  75.      long to StringBuffer object at specified offset
  76.     */
  77.     long l = 10;
  78.     StringBuffer sb7 =  new StringBuffer("Hello  World");
  79.     sb7.insert(6,l);
  80.     System.out.println(sb7);
  81.    
  82.     /*
  83.      StringBuffer insert(int offset, Object obj) method inserts
  84.      Object to StringBuffer object at specified offset
  85.     */
  86.     Object obj = new String("My");
  87.     StringBuffer sb8 =  new StringBuffer("Hello  World");
  88.     sb8.insert(6,obj);
  89.     System.out.println(sb8);
  90.  
  91.     /*
  92.      StringBuffer insert(int offset, String str) method inserts
  93.      String to StringBuffer object at specified offset
  94.     */
  95.     String str = "New";
  96.     StringBuffer sb9 =  new StringBuffer("Hello  World");
  97.     sb9.insert(6,str);
  98.     System.out.println(sb9);
  99.    
  100.     /*
  101.      NOTE: Above all method throws StringIndexOutOfBoundsException if the
  102.      offset is less than 0 or grater than length of StringBuffer object.  
  103.     */
  104.   }
  105. }
  106.  
  107. /*
  108. Output would be
  109.  
  110. Hello true World
  111. Hello Y World
  112. Hello Yes World
  113. Hello 1.0 World
  114. Hello 2.0 World
  115. Hello 5 World
  116. Hello 10 World
  117. Hello My World
  118. Hello New World
  119.  
  120. */