String str1="your're";
String str2=str1.subString(0,5)+"''"+str1.subString(5,7);

解决方案 »

  1.   

    楼上,如果String 里头有很多',如何处理,例如 your're'your're'your're'your're,总不能靠substring,'在string中的位置不是固定的
      

  2.   

    String s="your're";//最好写成"your\'re", and your're means what?
    s.replace('\'','\"');
      

  3.   

    楼上,我接收的信息是来自合作公司通过socket发送过来的,并要插入SQLServer数据库,主要是sql语句中'的转义符问题.
      

  4.   

    //字符串完全替换函数
    /**
     * String sOld要被替换的字符串(')
     * String sNew替换成的字符串('')
     * String sLine整个字符串(your'reyour'reyour'reyour're)
     */
    String ReplaceAllStr(String sOld,String sNew,String sLine)
    {
      int iPosition;
      iPosition=sLine.indexOf(sOld,0);
      String sLeft,sRight;
      while(iPosition!=-1)
        {
          sLeft=sLine.substring(0,iPosition);
          sRight=sLine.substring(iPosition+sOld.length());
          sLine=sLeft+sNew+sRight;
          iPosition=iPosition+sNew.length();
          iPosition=sLine.indexOf(sOld,iPosition);
        }
      return sLine;
    }
      

  5.   

    String.replace('\'','\"');
    replace all ' into " directly
    \' ==', \" ==", either of them is one char not two characters
      

  6.   

    From java.lang.String.java
        /**
         * Returns a new string resulting from replacing all occurrences of
         * <code>oldChar</code> in this string with <code>newChar</code>.
         * <p>
         * If the character <code>oldChar</code> does not occur in the
         * character sequence represented by this <code>String</code> object,
         * then a reference to this <code>String</code> object is returned.
         * Otherwise, a new <code>String</code> object is created that
         * represents a character sequence identical to the character sequence
         * represented by this <code>String</code> object, except that every
         * occurrence of <code>oldChar</code> is replaced by an occurrence
         * of <code>newChar</code>.
         * <p>
         * Examples:
         * <blockquote><pre>
         * "mesquite in your cellar".replace('e', 'o')
         *         returns "mosquito in your collar"
         * "the war of baronets".replace('r', 'y')
         *         returns "the way of bayonets"
         * "sparring with a purple porpoise".replace('p', 't')
         *         returns "starring with a turtle tortoise"
         * "JonL".replace('q', 'x') returns "JonL" (no change)
         * </pre></blockquote>
         *
         * @param   oldChar   the old character.
         * @param   newChar   the new character.
         * @return  a string derived from this string by replacing every
         *          occurrence of <code>oldChar</code> with <code>newChar</code>.
         */
        public String replace(char oldChar, char newChar) {
    if (oldChar != newChar) {
        int len = count;
        int i = -1;
        char[] val = value; /* avoid getfield opcode */
        int off = offset;   /* avoid getfield opcode */     while (++i < len) {
    if (val[off + i] == oldChar) {
        break;
    }
        }
        if (i < len) {
    char buf[] = new char[len];
    for (int j = 0 ; j < i ; j++) {
        buf[j] = val[off+j];
    }
    while (i < len) {
        char c = val[off + i];
        buf[i] = (c == oldChar) ? newChar : c;
        i++;
    }
    return new String(0, len, buf);
        }
    }
    return this;
        }