我们在浏览器上输入网址的时候,经常采用类似下面的形式 http: / /news . sina. com. cn/c/2010-08-09/ 170620859060. shtml它可以

package chengxusheji;
import java.util.Scanner;
public class A {
	public static void main(String args[]) {
		Scanner in=new Scanner(System.in);
		String str=in.nextLine();
		
		//str="http://news.sina.com.cn/c/2010-08-09/170620859060.shtml";
		
		//获取第一次出现指定字符串处的索引
		int a=str.indexOf("//");
		
		//获取指定索引开始第一次出现指定字符串处的索引
		int b=str.indexOf("/",7);
		
		//获取最后一次出现指定字符串处的索引
		int c=str.lastIndexOf("/");
		
		//获取指定索引之间的字符串
		System.out.println(str.substring(0,a)+"//");
		System.out.println(str.substring(a+2,b+1));
		System.out.println(str.substring(b+1,c+1));
		
		//获取从指定索引往后的所有字符串
		System.out.println(str.substring(c+1));
	}
}

猜你喜欢

转载自blog.csdn.net/qq_36925114/article/details/84206134
今日推荐