-2

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

I tried using

StringA=StringA.replaceAll(''','"');
Kumara
  • 117
  • 2
  • 10
  • 2
    `StringA=StringA.replaceAll("'","\"");` – Luiggi Mendoza May 11 '14 at 07:37
  • Your example won't compile. Simple googling would give you the answer you need. Why bother to asking the question here? I am talking that this is silly question, it is not. But you may get the answer much faster. – swist May 11 '14 at 07:57

3 Answers3

4

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    So what? BTW, it's not exactly the same, since mine doesn't recommend escaping the double quote, explains why the single quote must be escaped, suggests respecting naming conventions, and provides a link to the javadoc. If you're concerned that I borrowed your idea, I didn't. Actually, I commented about it before you posted your answer, in unknown's answer, in hope that he would fix his answer. But unknown didn't catch my comment, so I posted an answer while you were posting yours. – JB Nizet May 11 '14 at 08:02
3

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.

ethanfar
  • 3,733
  • 24
  • 43
  • 2
    Why escape the double quote? It doesn't need to be. – JB Nizet May 11 '14 at 07:47
  • You're right, but it's common to do that, since it's a special character. It's considered a better practice to escape the double quotes in a single quotation as well, even though, as you correctly mention, it isn't mandatory. – ethanfar May 11 '14 at 07:49
  • 1
    Considered by who? I find it clutters the code and makes it less readable. – JB Nizet May 11 '14 at 07:51
  • 1
    Well, to each his own. However, I don't really see how a single backslash character "clutters" code. – ethanfar May 11 '14 at 07:53
1

Use \ before double quote.

StringA = StringA.replaceAll("'","\"");
unknown
  • 4,859
  • 10
  • 44
  • 62
  • 1
    It's replace, not replaceAll. replaceAll takes two strings as argument, and uses a regexp. And why use a regexp to replace a single char by another one, since a dedicated method exists for that (and is obviously faster). – JB Nizet May 11 '14 at 07:39
  • first string is `'` (single quote) and second string is `"`(double quote). – unknown May 11 '14 at 07:41
  • Your original answer took chars as arguments, but used the wrong method name. – JB Nizet May 11 '14 at 07:41
  • I have updated it. It can not be character. ty for pointing out. – unknown May 11 '14 at 07:42