使用 new Remover().remove(String),
jdb@Vigor14:/tmp/stackoverflow> javac Remover.java && java Remover
folder > folder
hello.txt > hello
read.me > read
hello.bkp.txt > hello.bkp
weird..name > weird.
.hidden > .hidden
卸妆.java,
import java.util.*;
public class Remover {
public static void main(String [] args){
Map<String, String> tests = new LinkedHashMap<String, String>();
tests.put("folder", "folder");
tests.put("hello.txt", "hello");
tests.put("read.me", "read");
tests.put("hello.bkp.txt", "hello.bkp");
tests.put("weird..name", "weird.");
tests.put(".hidden", ".hidden");
Remover r = new Remover();
for(String in: tests.keySet()){
String actual = r.remove(in);
log(in+" > " +actual);
String expected = tests.get(in);
if(!expected.equals(actual)){
throw new RuntimeException();
}
}
}
private static void log(String s){
System.out.println(s);
}
public String remove(String in){
if(in == null) {
return null;
}
int p = in.lastIndexOf(".");
if(p <= 0){
return in;
}
return in.substring(0, p);
}
}