2013-05-03 2 views
-4

나는 파일 이름을 문자열로 읽습니다. 이 파일 이름의 길이는 얼마인지 알 수 없지만 .txt로 끝나는 것을 알고 있습니다. .txt 파일 이름 만 남기고 싶습니다. 어떤 아이디어?파일 이름에서 .txt를 제거하십시오 - Java

+1

음 (또한 파일의 끝에 내부가 아니라 어떤 .txt 인 버려야합니다)에있다 문자열이 * .txt로 끝나는 지 확인하는 분명한 접근법과 원래 길이 - 4 ... 그걸 시도해 봤어? –

+0

http://stackoverflow.com/questions/941272/how-do-i-trim-a-file-extension-from-a-string-in-java –

+1

* "아이디어가 있으십니까?"* 예. 물어보기 전에 포럼을 검색하고 약간의 노력을 보여주십시오. –

답변

0
If (filename.endsWith(".txt")) { 
    filename = filename.substring(0, filename.length()-4); 
} 

또는 :

filename = filename.replace(".txt", ""); 

+0

'filename = filename.replaceFirst ("\\. txt $", "");' –

0
yourString.subtring(0, yourString.length()-4); 
0
if(filepath.endsWith(".txt") { 
    filepath = filepath.substring(0, filepath.lastIndexOf(".txt"); 
} 
1
int dot = file.getFileName().toString().lastIndexOf("."); 
String me = "File: " + file.getFileName().toString(); 
System.out.println("File: " + file.getFileName().toString().substring(0,dot+1)); 
관련 문제