-1

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"
Hari
  • 441
  • 6
  • 15
  • Belongs on http://codereview.stackexchange.com/ – OneCricketeer Jan 15 '16 at 17:01
  • The input string starts always with `removeString`? – Francesco Pitzalis Jan 15 '16 at 17:03
  • Not exactly. If the removeString ="!a", then the output should be the same. Each character in the removeString should be checked with each character in inputString from left and remove until you find a character that does not exists in removeString. At that point it needs to be stopped and print the remaining string in inputString – Hari Jan 15 '16 at 17:04
  • I have added steps to follow in the question. – Hari Jan 15 '16 at 17:17

3 Answers3

2

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.

Brian H.
  • 2,092
  • 19
  • 39
  • add validation for inputString length, otherwise you will throw exception on empty string or string where all symbols should be removed for example – Iłya Bursov Jan 15 '16 at 18:14
0

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);
}
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
  • Could you briefly explain the performance difference between converting to a Set vs. comparing Char to Char? – Brian H. Jan 15 '16 at 17:42
  • @BrianHVB my version has average complexity O(N), comparing every char from input string with every char in remove string - O(N*N) – Iłya Bursov Jan 15 '16 at 18:12
-1

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

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • I think you have it backwards. The OP needs to have any characters contained in a target string removed from the the beginning of the input string. – Brian H. Jan 15 '16 at 17:45
  • This assumes that the token is exactly one character, which is not true given the example in the question. – Mage Xy Jan 15 '16 at 17:45
  • @MageXy Since when? https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith%28java.lang.String%29 `startsWith` accepts string not char. – Antoniossss Jan 15 '16 at 19:13
  • Actually, I was focused on the `substring(1)` part - I thought it had to be `substring(myStopToke.length)`. But now that I look at it further, your code doesn't give the right output either way - it leaves the token in the string. OP wanted it removed. See the [Java fiddle](http://goo.gl/1xuYPC). – Mage Xy Jan 15 '16 at 19:23
  • @MageXy True, and that is intended (from me side). Iv added modified example as well. – Antoniossss Jan 15 '16 at 19:29