这里给出字符串的两个位置,pos1和pos2,
如何替换pos1和pos2之间的字串?不要用正则表达式,简单则好

解决方案 »

  1.   

    String.substring(0,pos1)+newString+String.substring(pos2+1)
      

  2.   

    用substring
    substring(0,pos1) //pos1前面的字符串
    substring(pos2+1) //pos2+1位置起到字符串尾
      

  3.   

    笨且直观的方法。String testStr = "aaacwwqe";
    int pos1 = 1;
    int pos2 = 5;
    char [] charArray = testStr.toCharArray();
    char tempStr = charArray[pos1-1];
    charArray[pos1-1] = charArray[pos2-1];
    charArray[pos2-1] = tempStr;
    StringBuilder stb = new StringBuilder();
    for (char ch:charArray) {
    stb.append(String.valueOf(ch));
    }
    System.out.println("The replace string is:" + stb.toString());