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

前端 未结 9 670
广开言路
广开言路 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:38

    How about using regex?

    String firstWord = filename.replaceAll("\\..*","")
    

    This replaces everything from the first dot to the end with "" (ie it clears it, leaving you with what you want)

    Here's a test:

    System.out.println("abc.def.hij".replaceAll("\\..*", "");
    

    Output:

    abc
    

提交回复
热议问题