In java how to get substring from a string till a character c?

前端 未结 9 676
广开言路
广开言路 2020-12-04 16:26

I have a string (which is basically a file name following a naming convention) abc.def.ghi

I would like to extract the substring before the first

9条回答
  •  醉话见心
    2020-12-04 16:44

    If your project already uses commons-lang, StringUtils provide a nice method for this purpose:

    String filename = "abc.def.ghi";
    
    String start = StringUtils.substringBefore(filename, "."); // returns "abc"
    

    see javadoc [2.6] [3.1]

提交回复
热议问题