java獲取檔案字尾名

2021-07-07 10:13:11 字數 311 閱讀 2141

/**獲取檔案字尾名**/

public static string endwith (string filename)

}return result;

}可以放到工程的工具類裡直接呼叫,需要注意的是string token = filename.split("\\.");  一定要用轉義字元,因為spilt裡面有一定的正規表示式在裡面

發現個更簡單的模式並且上面的方法有問題   就是當有兩個點的檔名的時候

所以新的方法如下

public static string endwith(string filename)

獲取檔案字尾名

獲取到filename的字尾 利用lastindexof 從最後面截字尾,找到最後乙個點的索引然後加一,利用substring擷取該索引後的字串 f.getfilename substring f.getfilename lastindexof 1 lastindexof 方法有以下四種形式 pub...

獲取檔案的字尾名

使用api pathfindextension實現獲取給定乙個檔案,直接獲取檔案的字尾名 如 檔名 hsduiew.txt jhdsi.adiwey 路徑 檔名 c hsh shsh tetet.txt 都可以直接獲取到字尾名。pathfindextension函式說明 函式原型 ptstr pat...

PHP 獲取檔案字尾名

1.file x.y.z.png echo substr strrchr file,1 解析 strrchr file,strrchr 函式查詢字串在另乙個字串中最後一次出現的位置,並返回從該位置到字串結尾的所有字元 2.file x.y.z.png echo substr file,strrpos...