代码如下:
Scanner sc = new Scanner(System.in);
String url = sc.nextLine();
//通过":"作为分隔符将字符串进行分割,即分割为两个子字符串
String[] split = url.split(":");
String str = split[1];
int index = 0;
//在右半部分的字符串中出现字符'/'或者'\'进行跳过
//注'\'在java中为转义字符,实际表示'\'使用'\\'
while (str.charAt(index) == '/'|| str.charAt(index) == '\\' ){
index++;
}
//得到修改过后的右半部分字符串
str = str.substring(index);
//将字符串进行拼接
System.out.println(split[0]+"://"+str);
测试案例:
"https:\www.baidu.com"
"https://www.baidu.com"