String.replace('\'','\"'); replace all ' into " directly \' ==', \" ==", either of them is one char not two characters
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; }
s.replace('\'','\"');
/**
* 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;
}
replace all ' into " directly
\' ==', \" ==", either of them is one char not two characters
/**
* 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;
}