去哪儿笔试:filename extension

题目描述

Please create a function to extract the filename extension from the given path,return the extracted filename extension or null if none.
输入描述:
输入数据为一个文件路径
输出描述:
对于每个测试实例,要求输出对应的filename extension
示例1
输入

Abc/file.txt
输出

txt

思路:

考查字符串的常用方法

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String line = sc.nextLine();
int index = line.lastIndexOf('.') + 1;
String result = (index == 0) ? "null" : line.substring(index);
System.out.println(result);
}
sc.close();
}
}