Remove filename extension in Java

后端 未结 10 2258
你的背包
你的背包 2020-12-09 08:48

(Without including any external libraries.)

What\'s the most efficient way to remove the extension of a filename in Java, without assuming anything of the f

10条回答
  •  鱼传尺愫
    2020-12-09 09:26

    The remove() function above should be rewritten to support test cases like LOST.DIR/myfile.txt

        public static String removeExtension( String in )
    {
        int p = in.lastIndexOf(".");
        if ( p < 0 )
            return in;
    
        int d = in.lastIndexOf( File.separator );
    
        if ( d < 0 && p == 0 )
            return in;
    
        if ( d >= 0 && d > p )
            return in;
    
        return in.substring( 0, p );
    }
    

提交回复
热议问题