Skip to Content

Java String Split 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 String split example.
  3. This Java String split example describes how Java String is split into multiple
  4. Java String objects.
  5. */
  6.  
  7. public class JavaStringSplitExample{
  8.  
  9.   public static void main(String args[]){
  10.   /*
  11.   Java String class defines following methods to split Java String object.
  12.   String[] split( String regularExpression )
  13.   Splits the string according to given regular expression.
  14.   String[] split( String reularExpression, int limit )
  15.   Splits the string according to given regular expression. The number of resultant
  16.   substrings by splitting the string is controlled by limit argument.
  17.   */
  18.  
  19.   /* String to split. */
  20.   String str = "one-two-three";
  21.   String[] temp;
  22.  
  23.   /* delimiter */
  24.   String delimiter = "-";
  25.   /* given string will be split by the argument delimiter provided. */
  26.   temp = str.split(delimiter);
  27.   /* print substrings */
  28.   for(int i =0; i < temp.length ; i++)
  29.     System.out.println(temp[i]);
  30.  
  31.   /*
  32.   IMPORTANT : Some special characters need to be escaped while providing them as
  33.   delimiters like "." and "|".
  34.   */
  35.  
  36.   System.out.println("");
  37.   str = "one.two.three";
  38.   delimiter = "\\.";
  39.   temp = str.split(delimiter);
  40.   for(int i =0; i < temp.length ; i++)
  41.     System.out.println(temp[i]);
  42.  
  43.   /*
  44.   Using second argument in the String.split() method, we can control the maximum
  45.   number of substrings generated by splitting a string.
  46.   */
  47.  
  48.   System.out.println("");
  49.   temp = str.split(delimiter,2);
  50.   for(int i =0; i < temp.length ; i++)
  51.     System.out.println(temp[i]);
  52.  
  53.   }
  54.  
  55. }
  56.  
  57. /*
  58. OUTPUT of the above given Java String split Example would be :
  59. one
  60. two
  61. three
  62. one
  63. two
  64. three
  65. one
  66. two.three
  67. */