Remove a random expression from string

后端 未结 7 1725
春和景丽
春和景丽 2021-01-21 06:25

I have a string/column something like this

String a = \"000003023_AggregateStopLossLimit_W x3A 973911_2012-12-22.PDF\";

I want to create a subs

7条回答
  •  Happy的楠姐
    2021-01-21 06:56

    replaceAll takes a regex as an argument, if the substring contains regex markers (such as [, + for example) you will get an unexpected behaviour.

    You can use replace instead which does the same thing but takes a string as a parameter.

    Apart from that, if you know that you will have a space and a _ as delimiters, AND the substring in between does not occur elsewhere, then your approach looks fine. You could possibly make it slightly more readable with intermediate variables:

    int start = a.indexOf(" ");
    int end = a.indexOf("_", start);
    String b = a.substring(0, start) + a.substring(end, a.length());
    

提交回复
热议问题