-2

I want to remove xxxx-xxxx in string.

  1. String data = "help text 2015-2016 dummy string";

    Need output: help text dummy string

  2. String data = "abcd 2057-3827 any string";

Need output: abcd any string

Mohith Kumar
  • 617
  • 1
  • 5
  • 13
Arnish
  • 1
  • 1
  • 3
    Pure code-writing requests are off-topic on Stack Overflow -- we expect questions here to relate to *specific* programming problems -- but we will happily help you write it yourself! Tell us [what you've tried](http://whathaveyoutried.com), and where you are stuck. This will also help us answer your question better. – Elliott Frisch Feb 01 '16 at 04:35

1 Answers1

1

The following code should work:

String regexp = "[^\\s]{4}-[^\\s]{4}\\s";
String str = "help text 2015-2016 dummy string";
System.out.println(str.replaceFirst(regexp, ""));

If xxxx-xxxx is always a number, then you can use:

String r = "[0-9]{4}-[0-9]{4}\\s";
Dave F
  • 1,837
  • 15
  • 20