String s = "http://www.abcd.cn//123/233.aspx";
        int index = 0;
        String strStart = s.substring(0, s.indexOf("//") + 2);
        String strEnd = s.substring(s.indexOf("//") + 2, s.length());
        strEnd = strEnd.replace("//", "/");
        System.out.println(strStart);
        System.out.println(strEnd);没做一些越界检查,自己处理一下。

解决方案 »

  1.   

    public class Caofeng {

    public String func(String s){
    int index=s.lastIndexOf("//");
    String str=s.substring(0,index);
    str+="/";
    str+=s.substring(index+2);
    return str;
    }

    public static void main(String args[]){
    Caofeng c=new Caofeng();
    System.out.println(c.func("http://www.abcd.cn//123/233.aspx"));
    }}
      

  2.   

    如果 s = "http://www.abcd.cn//123//1234//233.aspx";
      

  3.   

    strEnd = strEnd.replaceAll("//", "/");
      

  4.   


    看看如何能用
    "http://www.abcd.cn//123/233.aspx".replaceAll(正则表达式,"/")
    解决
      

  5.   


    String s = "http://www.abcd.cn//123/233.aspx";
            s = s.replaceFirst("//", "/");
            s = s.replaceFirst("//", "/");
            s = s.replaceFirst("/", "//");
            System.out.println(s);
      

  6.   

    把http://先砍掉,然后再进行全部替换
      

  7.   


    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Replace{
    public static void main(String[] args){
    String content = "http://www.abcd.cn//123//233.aspx";
    String replacement = "/";
    String field = "//";
    int index = 2;
    String afterReplace = replace(content,field,replacement,index);
    System.out.println(afterReplace);
    } private static String replace(String content,String field,String replacement,int index){
    int time = 0;
    if(!content.contains(field) || ( index <= 0)){
    throw new IllegalStateException();
    } String regex = field;
    Pattern pattern = Pattern.compile(regex,Pattern.LITERAL);
    Matcher matcher = pattern.matcher(content); while(matcher.find()){
    time ++;
    if(time == index){
    content = content.substring(0,matcher.end() - regex.length()) + replacement + content.substring(matcher.end());
    return content;
    }
    }

    if(index > time){
    throw new IllegalStateException();
    }
    return null;
    }
    }
      

  8.   

    public class StringReplace { public static void main(String[] args) {
    String ss = "http://www.abcd.cn//123/233.aspx";
            System.out.println(func(ss, "//", "/", 2));
            
    }

    public static String func(String s, String regex, String replacement, int time){
            int index=0;
            for (int i=0; i<time; i++) {
             index = s.indexOf(regex, index) + 2;
             System.out.println("index=="+index);
            }
            index = index-regex.length();
            String str=s.substring(index);
            str = str.replaceFirst(regex, replacement);
            str = s.substring(0, index) + str;
            return str;
        }
    }
      

  9.   

    "http://www.abcd.cn//123//233.aspx".replaceAll("[^:]//", "/")