How to replace a special character with single slash
<p>I have a question about strings in Java. Let's say, I have a string like so:</p>

<pre><code>String str = "The . startup trace ?state is info?";
</code></pre>

<p>As the string contains the special character like <code>"?"</code> I need the string to be replaced with <code>"\?"</code> as per my requirement. How do I replace special characters with <code>"\"</code>? I tried the following way.</p>

<pre><code>str.replace("?","\?"); 
</code></pre>

<p>But it gives a compilation error. Then I tried the following:</p>

<pre><code>str.replace("?","\\?");
</code></pre>

<p>When I do this it replaces the special characters with  <code>"\\"</code>. But when I print the string, it prints with single slash. I thought it is taking single slash only but when I debugged I found that the variable is taking <code>"\\"</code>.</p>

<p>Can anyone suggest how to replace the special characters with single slash (<code>"\"</code>)?</p>
