两个字符串,一个是0000abc1111defabc
另外一个是,xyz
我希望在第一个字符串中出现的所有的abc全替换成xyz。
但是不许使用replace方法,已经折磨我很久了,有没有人能帮帮忙。
谢谢了

解决方案 »

  1.   

    用 indexOf 也可以 今天出了几个这样的帖子了  
    http://topic.csdn.net/u/20081214/08/4dc9f984-99f8-497d-ae22-fc357ff0e307.html
    这个帖子和你的差不多  
    看看 写出来应该没问题的
      

  2.   

    public class replaceTest { public static void main(String[] args) {
    String str1="0000abc1111defabc";
    String str2="xyz";
    while(str1.indexOf("000")!=-1){
    str1=str1.substring(0, str1.indexOf("000"))+ str2 + str1.substring(str1.indexOf("000")+3,str1.length());
    }
    System.out.println(str1); }}
      

  3.   


    public class Interview 
    {

    public static String replaceAll(String source,String from,String to) 
    {
    //source = source.replace(from,to);
    //return source;
    String [] str = new String [source.length() - from.length() + 1]; int i = 0;
    while(i <= source.length() - from.length())
    {
    str[i] = source.substring(i,i+from.length());
    if(str[i].equals(from))
    {
    source = source.substring(0,i) + to + source.substring(i+from.length());
    i += to.length();
    }
    else
    {
    i++;
    }
    }
    return source;
    } public static void main(String[] args) 
    {
    String source = "aaaaacafadfsfd";
    String from = "aaa";
    String to  = "ddd";
    String str = replaceAll(source,from,to);
    System.out.println(str);
    }
    }
      

  4.   


    public class Interview 
    {
    public static String replaceAll(String source,String from,String to) 
    {
    //source = source.replace(from,to);
    //return source;
    String [] str = new String [source.length() - from.length() + 1]; int i = 0;
    while(i <= source.length() - from.length())
    {
    str[i] = source.substring(i,i+from.length());
    if(str[i].equals(from))
    {
    source = source.substring(0,i) + to + source.substring(i+from.length());
    i += to.length();
    }
    else
    {
    i++;
    }
    }
    return source;
    } public static void main(String[] args) 
    {
    String source = "aaaaacafadfsfd";
    String from = "aaa";
    String to  = "ddd";
    String str = replaceAll(source,from,to);
    System.out.println(str);
    }
    }
      

  5.   

    String a = "0000abc1111defabc"; 
    String b = "xyz"; 
    Pattern p = Pattern.compile("abc"); 
    Matcher m = p.matcher(a); 
    StringBuffer result = new StringBuffer(); 
    while (m.find()) { 
    m.appendReplacement(result, b);//把替换后的字符串插入到result中 

    System.out.println(result);
      

  6.   


    import java.util.regex.Matcher; 
    import java.util.regex.Pattern; 
    public static void main(String []arg){ 
    String a = "abcd000def000cde000"; 
    String b = "111"; 
    Pattern p = Pattern.compile("000"); 
    Matcher m = p.matcher(a); 
    StringBuffer result = new StringBuffer(); 
    while (m.find()) { 
    m.appendReplacement(result, "111"); 

    System.out.println(result); 

      

  7.   


    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Test { public static void main(String[] args) {
       
    String begin = "0000abc1111defabc";

    Pattern pattern = Pattern.compile("abc");
    Matcher m = pattern.matcher(begin);

    StringBuffer newStr = new StringBuffer();
    while(m.find()){
    m.appendReplacement(newStr,"xyz");//实现非终端添加和替换步骤
    }
    m.appendTail(newStr);//实现终端添加和替换步骤。
    System.out.println(newStr.toString());
    }
    }