Java--截取路径字符串

来源:互联网 发布:mac hosts文件位置 编辑:程序博客网 时间:2024/05/17 23:16

转载自: http://blog.csdn.net/zlqqhs/article/details/8521861

1.截取路径最后一个字符串

 

[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. }  
0 0