如将"cde"插入到"abfgh"中的b后面,这个算法用java怎么写?求各位高手赐教!!!

解决方案 »

  1.   

    /**
     * @param 将字符串插入指定的位置
     * str被插入的字符串
     * substr要插入的字符串
     * location 要插入的位置
     */
    public static char[] getInsert(char[] str,char[] substr,int location){
    int str_length = str.length;
    int substr_length = substr.length;
    //判断是否是合法的位置
    if(location<0||location<str.length){
    return null;
    }
    //插入一个指定长度的字符数组
    char[] insertedStr = new char[str.length+substr.length];
    //前面的一段
    for(int i=0;i<location;i++){
    insertedStr[i] = str[i];
    }
    //插入的一段
    for(int i=0;i<substr_length;i++){
    insertedStr[i+location] = str[i];
    }
    //后面的一段
    for(int i=0;i<str_length;i++){
    insertedStr[i+substr_length] = str[i];
    }

    // System.out.print(insertedStr.toString());
    return (insertedStr);
    }怎么样把这个完整的字符串打印出来?求赐教!
      

  2.   

    import java.util.regex.*;
    public class Test2 { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub StringBuffer s1 = new StringBuffer("abfgh");
    String s2 = "cde";

    Pattern p = Pattern.compile("b");
    Matcher m = p.matcher(s1.toString());
    if(m.find()){
    s1.insert((m.start()+1), s2);
    }

    System.out.println(s1.toString());
    }}
      

  3.   

    不好意思,忘了格式,重贴import java.util.regex.*;
    public class Test2 { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub StringBuffer s1 = new StringBuffer("abfgh");  //原字符串
    String s2 = "cde";                            //要插入的字符串

    Pattern p = Pattern.compile("b");             //插入位置
    Matcher m = p.matcher(s1.toString());
    if(m.find()){
    s1.insert((m.start()+1), s2);        //插入字符串
    }

    System.out.println(s1.toString());
    }}
      

  4.   

    /* cde"插入到"abfgh"中的b后面*/
    public class insert{
    public static void main(String args[]) {
    String s = "abfgh";
    String s1 = "cde";
    int n = s.indexOf('b');// 获得字母b第一次出现时的下标
    StringBuffer a = new StringBuffer(s);
    StringBuffer b = a.insert(n+1,s1);
    System.out.println("将cde插入abfgh后为:" + b);
    /* 汉字串也适用*/
    String ss="豫剧";
    String ss1="(河南)";
    System.out.println(new StringBuffer(ss).insert(1,ss1));
    }
    }
    /*************输出***************/
    将cde插入abfgh后为:abcdefgh
    豫(河南)剧
      

  5.   

    package Bang;public class Test { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub System.out.println(getInsert("abfgh", "cde", 1));
    } /**
     * @param 将字符串插入指定的位置
     *            str被插入的字符串 substr要插入的字符串 location 要插入的位置
     */ public static String getInsert(String str, String substr, int location) {
    String s1 = str;
    String s2 = substr;
    int i = location;
    String insertedStr = s1.substring(0, i) + s2
    + s1.substring(i, s1.length()); return insertedStr; }
    }
      

  6.   

      用方法很简单,自己可以去查API!
     你的算法有问题..  
      

  7.   

       public static String insertString(String original,int offset,String destination){
         return new StringBuilder(original).insert(offset,destination).toString();
        } 
      

  8.   

    cde abfgh
    先把原来的 "abfgh" 从 'b' 处 分成两个子字符串 : "ab" 和 "fgh"
    然后 先合并 "ab"和"cde" 成 "abced"
    然后 再合并 "abced"和"fgh"  得出最后的 结果 "abcedfgh"这里的合并字符串,分解字符串,从String API上找吧,一看就会
      

  9.   

    package string;public class Insert
    {
        
        /**
         * @param args
         */
        public static void main(String[] args)
        {
            // TODO Auto-generated method stub如将"cde"插入到"abfgh"中的b后面,这个算法用java怎么写?
            String samll = "cde";
            String big = "abfgh";
            String last[]=big.split("b");
            System.out.println(last[0]+"b"+samll+last[1]);
        }    
    }
      

  10.   

    开眼了,不过用了正则表达式的API还是不错的,这个我很少用,其他的方法随便就想得到。
      

  11.   


    public class Insert{
    public static void main(String[] args){
    String s1 = "abfg";
    String s2 = "cde";
    StringBuffer b = new StringBuffer(s1).insert(2,s2);
    System.out.println(b);
    }
    }