Split method

StringutilsThe method split allows you to split a given string into an array of strings. Here is a set of examples that show you different kind of use of this method:

 

  • Example of extraction of the date from a given String:
static public Calendar exampleSplitMethod()
{

  Calendar date = new GregorianCalendar();
  String inputString="<2017.03.09 16:46:14 080 -0500><I><data><Information>";

  inputString=inputString.replaceFirst("<",""); //we delete the firt delimiter because,
                                               // split method will replace it with an empty value
  String delimiters = "[\\<.\\>\\s:]+";
  String[] tokens=inputString.split(delimiters,-1);

  Integer year = Integer.parseInt(tokens[0]);
  Integer month= Integer.parseInt(tokens[1]);
  Integer day= Integer.parseInt(tokens[2]);

  date.set(year,month,day);
  return date;

}

  

 

Add a comment