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"