比如一个字符串:aaabbb(dddddd)cccceeeffffff,
通过运算把(dddddd)这部分的内容移到eee和ffffff之间变成aaabbbcccceee(dddddd)ffffff这样,如果字符串不含括号(),都能移成功,但是有括号却不行,

解决方案 »

  1.   

    你先把“(”“)”replace成其他的没有歧义的标识符看行不行
    最后再置换回来
      

  2.   


    public class Test {
    public static String moveString(String oStr, String mStr, String str1, String str2){
    String str = "", tmp1, tmp2;
    int index = oStr.indexOf(mStr);
    if(index < 0){
    System.out.println("No string to move!");
    }
    else{
    tmp1 = oStr.substring(0, index);
    tmp2 = oStr.substring(index + mStr.length());
    int index1 = tmp1.indexOf(str1 + str2);
    int index2 = tmp2.indexOf(str1 + str2);
    if(index1 >= 0){
    index = tmp1.indexOf(str2);
    str = tmp1.substring(0, index) +
    mStr + tmp1.substring(index) + tmp2;
    }
    else if(index2 >= 0){
    index = tmp2.indexOf(str2);
    str = tmp1 + tmp2.substring(0, index) +
    mStr + tmp2.substring(index);
    }
    }
    return str;
    }
        public static void main(String[] args) {
         String str = "aaabbb(dddddd)cccceeeffffff";
         String str2 = "eeeffffffaaabbb(dddddd)cccc";
            System.out.println(moveString(str, "(dddddd)", "eee", "ffffff"));
            System.out.println(moveString(str2, "(dddddd)", "eee", "ffffff"));
        }
    }