문제

I have a String containing: 'abc' 'abc' 'abc'. How can i use replaceAll, to produce: "abc" "abc" "abc" ?

I tried using

StringA=StringA.replaceAll(''','"');
도움이 되었습니까?

해결책

The method to replace every occurrence of a char by another char is replace().

The char literal for a single quote is '\'' (the single quote must be escaped, so that it's not interpreted as the end of the char literal).

So you want

s = s.replace('\'', '"');

replaceAll(), suggested by many other answers, replaces substrings matching a regexp by another substring. It's less appropriate than the method replacing a single char by another one.

Side note: please respect the Java naming conventions. Variables start with a lowercase letter. Only class names start with an uppercase letter.

다른 팁

StringA = StringA.replace('\'', '\"');

Even though it's called replace and not replaceAll, what it actually does is replace all occurrences of one character with the other:

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

This is more efficient than using the replaceAll version which replaces strings.

Use \ before double quote.

StringA = StringA.replaceAll("'","\"");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top