java截取文件上傳或下載的文件名及後綴

 

[html] view plaincopy
  1. /**  
  2.  * 截取鏈接最後一個字符串  
  3.  * @author ZLQ  
  4.  *  
  5.  */  
  6. public class StringTest {  
  7.  public static void main(String[] args) {  
  8.   String url = "http://zhidao.baidu.com/question/147458024.html";  
  9.   //取得最後一個/的下標  
  10.   int index = url.lastIndexOf("/");  
  11.   //將字符串轉爲字符數組  
  12.   char[] ch = url.toCharArray();  
  13.   //根據 copyValueOf(char[] data, int offset, int count) 取得最後一個字符串  
  14.   String lastString = String.copyValueOf(ch, index + 1, ch.length - index - 1);  
  15.   System.out.println(lastString);  
  16.  }  
  17. }  


 

 

2.截取鏈接最後一個字符串

 

[html] view plaincopy
  1. /**  
  2.  * 截取鏈接最後一個字符串  
  3.  * @author ZLQ  
  4.  *  
  5.  */  
  6. public class StringTest3 {  
  7.  public static void main(String[] args) {  
  8.   String url = "http://zhidao.baidu.com/question/147458024.html";  
  9.   //取得最後一個/的下標  
  10.   int index = url.lastIndexOf("/");  
  11.   //substring(int beginIndex)返回一個新的字符串,它是此字符串的一個子字符串。  
  12.   String newString = url.substring(index + 1);  
  13.   System.out.println(newString);  
  14.  }  
  15. }  


 

 

3. 截取/之間的字符串

 

[html] view plaincopy
  1. /**  
  2.  * 截取/之間的字符串  
  3.  * @author ZLQ  
  4.  *  
  5.  */  
  6. public class StringTest2 {  
  7.  public static void main(String[] args) {  
  8.   String url = "http://zhidao.baidu.com/question/147458024.html";  
  9.   //將字符串以/切分並存到數組中  
  10.   String[] split = url.split("/");  
  11.   for(String str : split){  
  12.    System.out.println(str);  
  13.   }  
  14.  }  
  15. }  
發佈了135 篇原創文章 · 獲贊 231 · 訪問量 69萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章