- /**
- * 截取鏈接最後一個字符串
- * @author ZLQ
- *
- */
- public class StringTest {
- public static void main(String[] args) {
- String url = "http://zhidao.baidu.com/question/147458024.html";
- //取得最後一個/的下標
- int index = url.lastIndexOf("/");
- //將字符串轉爲字符數組
- char[] ch = url.toCharArray();
- //根據 copyValueOf(char[] data, int offset, int count) 取得最後一個字符串
- String lastString = String.copyValueOf(ch, index + 1, ch.length - index - 1);
- System.out.println(lastString);
- }
- }
2.截取鏈接最後一個字符串
- /**
- * 截取鏈接最後一個字符串
- * @author ZLQ
- *
- */
- public class StringTest3 {
- public static void main(String[] args) {
- String url = "http://zhidao.baidu.com/question/147458024.html";
- //取得最後一個/的下標
- int index = url.lastIndexOf("/");
- //substring(int beginIndex)返回一個新的字符串,它是此字符串的一個子字符串。
- String newString = url.substring(index + 1);
- System.out.println(newString);
- }
- }
3. 截取/之間的字符串
- /**
- * 截取/之間的字符串
- * @author ZLQ
- *
- */
- public class StringTest2 {
- public static void main(String[] args) {
- String url = "http://zhidao.baidu.com/question/147458024.html";
- //將字符串以/切分並存到數組中
- String[] split = url.split("/");
- for(String str : split){
- System.out.println(str);
- }
- }
- }