Java String Split Example

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

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




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

escape

the carrot symbol(^) must be escaped as well.

thanx

thanx

applet

lots of examples on applet can be given


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




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