Skip to content
Advertisement

Split string containg dash/hyphen character in Java [closed]

I have a string

May 1988 – June 1992

I want to split this string in such way that I will get two separate strings May 1988 and June 1992 .

I tried the following code:

String sample="May 1988 – June 1992";
String[] arr=sample.split(" - ");

Advertisement

Answer

The String you are trying to split has a “en dash” (U+2013), while you try to split with a “HYPHEN-MINUS” (U+002D). Seeing the difference with your eyes is pretty hard (dash is slighly longer), but they are different symbols.

just copy and paste the en dash into your split method and it should work:

String[] arr=sample.split(" – ");
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement