user100 user100 - 1 year ago 84
Java Question

Split string containg dash/hyphen character in Java

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 following code

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


Answer Source

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(" – ");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download