-1

I have input string array containing value like

1950/00/00;1953/00/00;1958/00/00;1960/00/00;1962/0

What could be the best way to obtain values out of above input which contains 1950/00/00, 1953/00/00, 1958/00/00 , 1960/00/00 and 1962/0 in individual string objects?

Santhosh
  • 8,045
  • 2
  • 26
  • 54
user2488578
  • 806
  • 3
  • 12
  • 32

3 Answers3

2

Use the method String.split(regex):

String input = "1950/00/00;1953/00/00;1958/00/00;1960/00/00;1962/0";

String[] parts = input.split(";");

for (String part : parts) {
    System.out.println(part);
}
Jesper
  • 186,095
  • 42
  • 296
  • 332
1

The split() method splits the string based on the given regular expression or delimiter, and returns the tokens in the form of array. Below example shows splitting string with (;)

public class MyStrSplit {

    public static void main(String a[]){

        String str = "1950/00/00;1953/00/00;1958/00/00;1960/00/00;1962/0";
        String[] tokens = str.split(";");
        for(String s:tokens){
            System.out.println(s);
        }
    }
}
  • Could you please elaborate more your answer adding a little more description about the solution you provide? – abarisone Jun 16 '15 at 07:58
  • The split() method splits the string based on the given regular expression or delimiter, and returns the tokens in the form of array –  Jun 16 '15 at 08:04
0

Another choice to split string by regular expression:

public class SpitByRegx
{
  public static void main(String[] args)
  {
    String input = "1950/00/00;1953/00/00;1958/00/00;1960/00/00;1962/0";

    Pattern pattern = Pattern.compile("([0-9/]+);?");

    Matcher m = pattern.matcher(input);

    while(m.find())
    {
        System.out.println(m.group(1));
    }
  }
}
Javakid
  • 257
  • 1
  • 10