Search code examples
javastringjava-7java-6

Removing characters from left until another character appears. Need best way for this


I need to write an string utility function where i have to remove characters until another character appears.

Here is the example,

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

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

in this case the output should be *!b7!123!a!.

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

Any advice please,

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<charArray.length;i++)
    {
    if (flag && 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"

Solution

  • Here is my solution in condensed form. It is fairly performant, although I haven't analyzed the performance overhead of indexOf(Char c) in detail.

    1. Initialize a counter outside of the loop so it can be accessed later
    2. Loop through each single character in your inputString and match it to any character in the removeString.
    3. Continue until the first non-match is found at position j.
    4. Return a substring of your inputString from j to end (inclusive).

      int j;
      for (j = 0; removeString.indexOf(inputString.charAt(j)) != -1; j++){}
      String result = inputString.substring(j);
      System.out.println(result);
      

    The main difference between my code and yours is that once no matches are found, the process stops. Your code is building up the output character by character from start to end. This will carry a significant performance penalty, depending on how large and how many string there are.If I understand your requirements correctly, you are only looking at characters starting from the left, and stopping once a character no longer matches, thus any solution should also stop once there are no more matches.