Implementing a method that escapes single quotes in a string Background: In tcsh we can use single quotes inside single quote like this (like it said here):
echo 'It'\''s Shell Programming'

I want to create a method which escapes single quotes and prints it into a tcsh script I build on fly with Java. It should do:

If there is a \' (two chars) in the string, it will escape it so: \'\''.
If there is a ' (one char with no \ before it)  in the string, it will escape it so '\''.

I wrote the following method to do so:
private static String escapeStr(final String str) {
    String result = &quot;&quot;;
    for (int index = 0; index &lt; str.length(); ++index) {
        if (str.charAt(index) == '\\') {
            if (index + 1 &lt; str.length() &amp;&amp; str.charAt(index + 1) == '\'') {
                result += &quot;\\'\\\''&quot;;
                index++;
            } else {
                result += str.charAt(index);
            }
        } else if (str.charAt(index) == '\'') {
            result += &quot;'\\\''&quot;;
        } else {
            result += str.charAt(index);
        }
    }
    return result;
}

It looks like it does the job but I really don't like how I implemented it. It's hard to read as it has quite a lot of \ chars. Also I do index++ inside the  loop which feels like bad design. Is there a better approach here?
I will add that I want to call the method like so:
fileWriter.write(&quot;echo '&quot; + escapeStr(cmd) + &quot;'\n&quot;);

The whole point of this part is so the escape will print the cmd as-is. I wrap the command with ' so it won't evaluate stuff like environment variables. So if the cmd already contains single quote it will fail. I will need to escape single quotes and that what I tried to do.
I'd suggest a Solution based on regular expressions like this:
import static org.junit.Assert.assertThat;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hamcrest.CoreMatchers;
import org.junit.Test;

public class EscapeQuotesTest {

    Pattern escapedQuotePattern = Pattern.compile(&quot;(\\\\)?(')(')&quot;);

    private String escapeString(String input) {
        Matcher escapedQuote = escapedQuotePattern.matcher(input);
        escapedQuote.find();
        Optional&lt;String&gt; escapeChar = Optional.ofNullable(escapedQuote.group(1));
        String result = String.format(&quot;%s%s\\'%s&quot;, escapeChar.orElse(&quot;&quot;), escapedQuote.group(2), escapedQuote.group(3));
        return result;
    }

    @Test
    public void preseveLeadingBackslash() {
        String input = &quot;\\''&quot;;
        String result = escapeString(input);
        assertThat(&quot; The escape char survived&quot;, result, CoreMatchers.containsString(&quot;\\'\\''&quot;));
    }

    @Test
    public void noLeadingBackslash() {
        String input = &quot;''&quot;;
        String result = escapeString(input);
        assertThat(&quot;no escape char at beginning&quot;, result, CoreMatchers.containsString(&quot;'\\''&quot;));
    }
}