Java code to delete comments from an input string I wrote this code to delete comments from an input string (such as a Java file converted to string) and it works great. For simple curiosity, I was wondering if there was a more elegant way of presenting the function that I wrote. 

I'm looking for some advice on presenting the code, and how to condense the code.

Here's the function:

public static String deleteComments(String myString) {
    String newString = "";

    if (myString.contains("\"")) {
        if (myString.indexOf("\"") != 0) {

            String[] stringParts = myString.split("\"");

            for (int i = 0; i &lt; stringParts.length; i++) {

                if ((i &amp; 1) == 0) {
                    Pattern commentaryPattern = Pattern.compile("(/\\*((.|\n)*?)\\*/)|//.*");

                    Matcher m = commentaryPattern.matcher(stringParts[i]);

                    newString += m.replaceAll("");
                } else {
                    newString += "\"" + stringParts[i] + "\"";
                }
            }
        }
    } else {
        Pattern commentaryPattern = Pattern.compile("(/\\*((.|\n)*?)\\*/)|//.*");

        Matcher m = commentaryPattern.matcher(myString);

        newString += m.replaceAll("");
    }


    return newString;
}


Note: the regex pattern comes from another Stack Overflow link. It does most of the work but also deletes comments inside a string 
(eg: String myString = "bob said, \"Hi //To his friendly neighbor\".";) which, according to the java doc, is not a comment.
Advice 1

newString += m.replaceAll("");


This is inefficient. Use StringBuilder for literally building the strings. Your approach works, but if you += \$n\$ characters one at a time, the time complexity is \$\Theta(n^2)\$. With StringBuilder it would be \$\Theta(n)\$.

Alternative implementation

The following is by no means a suggested implementation, but rather a starting point:

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

public class Main {

    public static String deleteComments2(String str) {
        StringBuilder sb = new StringBuilder(str.length());
        boolean inText = false;
        char previousCharacter = '\u0000';

        for (int i = 0; i &lt; str.length(); ++i) {
            char currentCharacter = str.charAt(i);

            if (inText) {
                if (currentCharacter == '\"' &amp;&amp; previousCharacter != '\\') {
                    inText = false;
                }
            } else {
                if (currentCharacter == '\"') {
                    if (previousCharacter == '\\') {
                        throw new RuntimeException("bad syntax");
                    }

                    inText = true;
                }

                if (currentCharacter == '/' &amp;&amp; previousCharacter == '/') {
                    sb.deleteCharAt(sb.length() - 1);
                    break;
                }
            }

            sb.append(currentCharacter);
            previousCharacter = currentCharacter;
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            String str = scanner.nextLine();

            if (str.equals("quit")) {
                break;
            }

            System.out.println("1&gt; " + deleteComments(str));
            System.out.println("2&gt; " + deleteComments2(str));
            System.out.println("---");
        }
    }

    public static String deleteComments(String myString) {
        String newString = "";

        if (myString.contains("\"")) {
            if (myString.indexOf("\"") != 0) {

                String[] stringParts = myString.split("\"");

                for (int i = 0; i &lt; stringParts.length; i++) {

                    if ((i &amp; 1) == 0) {
                        Pattern commentaryPattern = Pattern.compile("(/\\*((.|\n)*?)\\*/)|//.*");

                        Matcher m = commentaryPattern.matcher(stringParts[i]);

                        newString += m.replaceAll("");
                    } else {
                        newString += "\"" + stringParts[i] + "\"";
                    }
                }
            }
        } else {
            Pattern commentaryPattern = Pattern.compile("(/\\*((.|\n)*?)\\*/)|//.*");

            Matcher m = commentaryPattern.matcher(myString);

            newString += m.replaceAll("");
        }

        return newString;
    }
}