Tuesday, August 17, 2010

string split using java

tokens -word,paragraph,sentences
delimiter -" " | "-" | ", " | "6"


String str = "one-two-three";

String[] temp;


String delimiter = "-";


temp = str.split(delimiter);


for(int i =0; i < temp.length ; i++)

System.out.println(temp[i]);

-------------------------------------------------------------------------------------------
note: IMPORTANT : Some special characters need to be escaped while providing them as

delimiters like "." and "|".



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


temp = str.split(delimiter,2);

for(int i =0; i < temp.length ; i++)

System.out.println(temp[i]);

output

one
two
three
------
one
two.three

No comments:

Post a Comment