Java StringBuffer insert method Example

  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. */

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!