filename extension

Topic description

Please create a function to extract the filename extension from the given path,return the extracted filename extension or null if none.

Enter description:

input data as a file path

Output description:

For each test instance, the corresponding filename extension is required to be output
Example 1

enter

Abc/file.txt

output

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("\\."); // . is an escape character and cannot be written directly 
15          if (s1.length>1 ) {
 16              s = s1[s1.length-1 ];
 17          } else {
 18              s= null ;
 19          }    
 20          System.out.println(s);
 21      }
 22 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325263433&siteId=291194637