- /**
- * 截取链接最后一个字符串
- * @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);
- }
- }
- }