输入数据为一个文件路径
对于每个测试实例,要求输出对应的filename extension
Abc/file.txt
txt
1 import java.util.Scanner; 2 /** 3 * 4 * Please create a function to extract the filename extension from the given path, return the extracted filename extension or null if none. 5 * 1、用"/" 分割取最后一项 6 * 2、用"." 分割取最后一项 7 */ 8 public class Main { 9 public static void main(String[] args) { 10 Scanner sc = new Scanner(System.in); 11 String s = sc.nextLine(); 12 String[] ss = s.split("/"); 13 s = ss[ss.length-1]; 14 String[]s1=s.split("\\."); // .是转义字符 不能直接编写 15 if (s1.length>1) { 16 s = s1[s1.length-1]; 17 }else { 18 s=null; 19 } 20 System.out.println(s); 21 } 22 }
原文:https://www.cnblogs.com/the-wang/p/8981527.html