Skip to Content

StringBuffer Trim Java 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.         StringBuffer Trim Java Example
  3.         This example shows how to trim StringBuffer object in Java using substring method.
  4. */
  5.  
  6. public class JavaStringBufferTrimExample {
  7.        
  8.         public static void main(String[] args) {
  9.                
  10.                 //create StringBuffer object
  11.                 StringBuffer sbf = new StringBuffer("   Hello World  !  ");
  12.                
  13.                 /*
  14.                  * Method 1: convert StringBuffer to string and use trim method of
  15.                  * String.
  16.                  */
  17.                
  18.                 String str = sbf.toString().trim();
  19.                
  20.                 System.out.println("StringBuffer trim: \"" + str +"\"");
  21.                
  22.                 /*
  23.                  * Method 2: Create method to trim contents of StringBuffer
  24.                  * using substring method.
  25.                  */
  26.                
  27.                 System.out.println("\"" + trim(sbf) + "\"");
  28.         }
  29.        
  30.         private static String trim(StringBuffer sbf){
  31.                
  32.                 int start, end;
  33.                
  34.                 //find the first character which is not space
  35.                 for(start = 0; start < sbf.length(); start++){
  36.                         if(sbf.charAt(start) != ' ')
  37.                                 break;
  38.                 }
  39.  
  40.                 //find the last character which is not space
  41.                 for(end = sbf.length(); end > start ; end--){
  42.                         if(sbf.charAt(end-1) != ' ')
  43.                                 break;
  44.                 }
  45.  
  46.                 return sbf.substring(start, end);
  47.                
  48.         }
  49. }
  50.  
  51. /*
  52. Output of above given StringBuffer trim example would be
  53. StringBuffer trim: Hello World  !
  54. Hello World  
  55. */