Java StringBuffer delete remove character or clear content Example

  1. /*
  2.   Java StringBuffer delete remove character or clear content Example
  3.   This example shows how to delete or remove a particular character or clear
  4.   entire content of a StringBuffer object.
  5. */
  6.  
  7. public class JavaStringBufferDeleteExample {
  8.  
  9. public static void main(String[] args) {
  10. /*
  11.   Java StringBuffer class following methods to delete / remove characters
  12.   or claring the contents of a StringBuffer object.
  13.   */
  14.  
  15. /*
  16.   StringBuffer delete(int start, int end) remove the characters from start
  17.   index to an end-1 index provided.
  18.   This method can throw a StringIndexOutOfBoundException if the start
  19.   index is invalid.
  20.   */
  21. StringBuffer sb1 = new StringBuffer("Hello World");
  22. sb1.delete(0,6);
  23. System.out.println(sb1);
  24.  
  25. /*
  26.   To clear contents of a StringBuffer use delete(int start, int end) method
  27.   in the below given way
  28.   */
  29. StringBuffer sb2 = new StringBuffer("Some Content");
  30. System.out.println(sb2);
  31. sb2.delete(0, sb2.length());
  32. System.out.println(sb2);
  33.  
  34. /*
  35.   StringBuffer deleteCharAt(int index) deletes the character at specified
  36.   index.
  37.   This method throws StringIndexOutOfBoundException if index is negative
  38.   or grater than or equal to the length.
  39.   */
  40. StringBuffer sb3 = new StringBuffer("Hello World");
  41. sb3.deleteCharAt(0);
  42. System.out.println(sb3);
  43. }
  44. }
  45.  
  46. /*
  47. Output would be
  48.  
  49. World
  50. Some Content
  51.  
  52. ello World
  53.  
  54. */

Simplest example

great! :D

how to find whitespaces??

is there any method to find whitespaces in a string??

thx

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!