1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
/* Java String split example. This Java String split example describes how Java String is split into multiple Java String objects. */ public class JavaStringSplitExample{ public static void main(String args[]){ /* Java String class defines following methods to split Java String object. String[] split( String regularExpression ) Splits the string according to given regular expression. String[] split( String reularExpression, int limit ) Splits the string according to given regular expression. The number of resultant substrings by splitting the string is controlled by limit argument. */ /* String to split. */ String str = "one-two-three"; String[] temp; /* delimiter */ String delimiter = "-"; /* given string will be split by the argument delimiter provided. */ temp = str.split(delimiter); /* print substrings */ for(int i =0; i < temp.length ; i++) System.out.println(temp[i]); /* IMPORTANT : Some special characters need to be escaped while providing them as delimiters like "." and "|". */ System.out.println(""); str = "one.two.three"; delimiter = "\."; temp = str.split(delimiter); for(int i =0; i < temp.length ; i++) System.out.println(temp[i]); /* Using second argument in the String.split() method, we can control the maximum number of substrings generated by splitting a string. */ System.out.println(""); temp = str.split(delimiter,2); for(int i =0; i < temp.length ; i++) System.out.println(temp[i]); } } /* OUTPUT of the above given Java String split Example would be : one two three one two three one two.three */ |
the carrot symbol(^) must be escaped as well.
thanx
lots of examples on applet can be given
thanks
Thanks
Thank you very much, very clear example and it helped me a lot to learn string handling in Java. Excellent code.
I find one more problem. Actually I have a pipe “|” separated string and if value is null then there is nothing between pipes(||); The problem is if in the last of string there is no character except pipes, then it ignore all pipes.
Suppose String name=”a|b|c|d||||||”
if we write String[] values=name.split(“[|]”) it gives only four element(a,b,c,d) in array. If I put a character in the last of the | seperated string it gives all(some null value in array). While StringUtils class of apache works fine.
Thanks a lot
Thank u….
Nice Example
Thanks and kind regards for providing the example on handling of special character in string split
I was trying to split on “.” and wondered why it wasn’t working
Simple and Great … you are rokin man
its clear
???????, ???????!!!
Awesome.
But wouldn’t it be more efficient to just declare:
String[] temp = str.split(“-“);
instead of:
creating a local variable temp,
declaring a local variable delimiter = “-“,
assigning temp = str.split(delimiter);
?
I’m saving 2 lines worth of work!
I’m new at programming so if anyone could tell me why they poster did it the long way?
just to explain it in various ways.
@Khoi Tran
It is not the long way it is the right way to make it easy to understand for the third person who is looking at your code.
thanks mas
ok, after I do all this while I go travel the array by storing it in an ArrayList, as I do that?
i dont get this stiiiiiiiiiiiiiil
long live asap
Thanks for the clear example.
What if I’m trying to split a string where the elements are delimited by: “\n\t\t” ?
I generate this string whit a DOM Parser so I can’t decide what to put between the readed elements, and by default the delimiter is the one I wrote above.
I wanted to know how this implementation would be in symfony2. Could any one please help me with this?
Source code for input like “Hello World Java” & output “HWJ”
Thanks a lot…..
suppose if we have two special characters in a string
can we split at a time (i.e) two operations
can you please explain?