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

前端 未结 9 672
广开言路
广开言路 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条回答
  •  旧时难觅i
    2020-12-04 16:39

    This could help:

    public static String getCorporateID(String fileName) {
    
        String corporateId = null;
    
        try {
            corporateId = fileName.substring(0, fileName.indexOf("_"));
            // System.out.println(new Date() + ": " + "Corporate:
            // "+corporateId);
            return corporateId;
        } catch (Exception e) {
            corporateId = null;
            e.printStackTrace();
        }
    
        return corporateId;
    }
    

提交回复
热议问题