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"

Answers


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.


You can optimize your solution by using set and only calculate index of first symbol, instead of string builder update every time:

private static String leftTrim(final String input, final String removeSymbols) {
    final Set<Integer> symbols = new HashSet<>(removeSymbols.length());
    removeSymbols.chars().forEach(c -> symbols.add(c));

    int idx = 0;
    while ((idx < input.length()) && symbols.contains((int) input.charAt(idx)))
        idx++;
    return input.substring(idx);
}

Accorting to the topic

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

Is this what u wanted?

while(myString.isEmpty()==false && myString.startsWith(myStopToke)==false){
    myString=myString.substring(1);
} 

this will remove every character from the left till myStopToken appears as begining of the string

Here you have live example. http://goo.gl/foj82G

And here you have sligh modification to exclude stopToken from the output http://goo.gl/TYVn1l


Need Your Help

What happens when a Flex App can't run at the specified framerate?

flex flash actionscript-3 performance

In our application (a game), in some cases it can't run fast enough. Obviously we'd like to speed it up, but in the mean-time when this happens it causes many problems (or if it's not causing them,...

Counting words from a mixed-language document

utf-8 multilingual cjk word-count

Given a set of lines containing Chinese characters, Latin-alphabet-based words or a mixture of both, I wanted to obtain the word count.