<p>I have following string</p>

<pre><code>String str = "replace :) :) with some other string";
</code></pre>

<p>And I want to replace first occurance of <code>:)</code> with some other string</p>

<p>And I used <code>str.replaceFirst(":)","hi");</code></p>

<p>it gives following exception</p>

<blockquote>
  <p>"Unmatched closing ')'"</p>
</blockquote>

<p>I tried using <code>replace</code> function but it replaced all occurance of <code>:)</code>.</p>

The
```
replaceFirst
```
method takes a regular expression as its first parameter. Since
```
)
```
is a special character in regular expressions, you must quote it. Try:
```
```
str.replaceFirst(":\\)", "hi");
```
```
The double backslashes are needed because the double-quoted string also uses backslash as a quote character.