Removing characters from left until another character appears. Need best way for this
<p>I need to write an string utility function where i have to remove characters until another character appears.</p>

<p>Here is the example,</p>

<pre><code>String inputString="a!a*!b7!123!a!";
String removeString="a!";
</code></pre>

<p>starting from the left of each character in inputString variable, i have to check if it presents in removeString. if it was present, i have to remove until another character appears</p>

<p>in this case the output should be <code>*!b7!123!a!.</code> </p>

<p>For this i have written java code but i am looking the best way to do it,</p>

<p>Any advice please,</p>

<pre><code>String inputString="a!a*!b7!123!a!";
String removeString="a!";
char[] charArray=inputString.toCharArray();
boolean flag=true;
StringBuilder stringBuilder=new StringBuilder();
for (int i=0;i&lt;charArray.length;i++)
    {
    if (flag &amp;&amp; removeString.contains((String.valueOf(charArray[i]))))
        {

        }
    else
        {
            flag=false;
            stringBuilder.append(String.valueOf(charArray[i]));
        }   
    }
    System.out.println(stringBuilder.toString());

String inputString="a!a*!b7!123!a!";
String removeString="a!";

Starting from the left of the inputString variable

Step 1: Get the first character from the inputString variable. It is 'a' and it is present in removeString so remove it. inputString="!a*!b7!123!a!"

Step 2: Get the second character of the step 1 result. It is '!' and it is present in removeString so remove it. inputString="a*!b7!123!a!"

Step 3: Get the third character of the step 2 result. It is 'a' and it is present in removeString so remove it. inputString="*!b7!123!a!"

Step 4: Get the fourth character of the step 3 result. It is '*' and it is NOT present in removeString so STOP.

Step 5: Final Output inputString="*!b7!123!a!"

The output should be the same evenn if removeString="!a"
</code></pre>