獲取檔案字尾名

2021-10-09 09:33:43 字數 1219 閱讀 2700

//獲取到filename的字尾  利用lastindexof()從最後面截字尾,找到最後乙個點的索引然後加一,

//利用substring擷取該索引後的字串

f.getfilename()

.substring

(f.getfilename()

.lastindexof

(".")+

1);

lastindexof()方法有以下四種形式:

public int lastindexof(int ch):

返回指定字元在此字串中最後一次出現處的索引,如果此字串中沒有這樣的字元,則返回 -1。

public int lastindexof(int ch, int fromindex):

返回指定字元在此字串中最後一次出現處的索引,從指定的索引處開始進行反向搜尋,如果此字串中沒有這樣的字元,則返回 -1。

public int lastindexof(string str):

返回指定子字串在此字串中最右邊出現處的索引,如果此字串中沒有這樣的字元,則返回 -1。

public int lastindexof(string str, int fromindex):

返回指定子字串在此字串中最後一次出現處的索引,從指定的索引開始反向搜尋,如果此字串中沒有這樣的字元,則返回 -1。

substring()方法返回字串的子字串(擷取字串)。

語法

public string substring(int beginindex)

或public string substring(int beginindex, int endindex)

引數

beginindex – 起始索引(包括), 索引從 0 開始。

endindex – 結束索引(不包括)。

返回值

子字串。

例項

public

class

test

}

以上程式執行結果為:

返回值 :runoob.com

返回值 :runoob

獲取檔案的字尾名

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

java獲取檔案字尾名

獲取檔案字尾名 public static string endwith string filename return result 可以放到工程的工具類裡直接呼叫,需要注意的是string token filename.split 一定要用轉義字元,因為spilt裡面有一定的正規表示式在裡面 發現...

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...