WHAT'S NEW?

Breaking the String into an array based on separator



Let say you have a string which reads "AX-001-2589". 

If you want to break the String into array based in the separator i.e. in this case is a -.

String have a method by name split().

The structure of the methods looks something like this

public String[] split(String regex)

- The above method splits the String into an array containing all the elements separated by the given regex.

So coming back to example if we need to split the String into the array the program goes like

class SplitTheString 
{
public static void main(String[] args) 
{
String str="AX-001-2526";

String[] array=str.split("-");

for(int i=0;i<array.length;i++)
{
System.out.println(array[i]);
}
}
}


So the array holds data something like this

array[0]="AX";
array[1]="001";
array[2]="2526";


0 comments:

Post a Comment