Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java String Split Example

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

hey, ive copied your program

hey, ive copied your program and ran it in my ready to program and got an error, that says "No method named "split" was found in type "java.lang.String"." can you maybe help me
Anonymous's picture

maybe you haven't include

maybe you haven't include java.util.*
Anonymous's picture

important part of string

important part of string split method is that it accept regular expression rather than simple string which gives you immense power on splitting. you can also see some string split examples here.
Anonymous's picture

I have got the same problem

I have got the same problem with split and I have include java.util.*
Anonymous's picture

It ran perfectly for me and I

It ran perfectly for me and I have also checked that there is split() method exists in String class itself . there are two even one take regex also. you can also check this post to learn more about String vs StringBuffer vs StringBuilder to know more about String.
Anonymous's picture

Some More Examples String

Some More Examples String Examples

Post new comment