Hey,
Im looking for the cleanest way to scan through a text file and remove symbols only if they meet certain conditions.
So far i have been using the Scanner, but it removes them regardless and i was looking for the best way of providing conditions like those for comments

remove - //to end of the line
or - /*remove until it finds another */

Should i provide these inside lots of if conditions or does scanner provide a neater way to do something like this? or maybe using a case statement?

public static String Stripsymbols(String line) {

        String strippedline="";
        String symbols = ".+;<>-=,*{}()";
        String search = null;

        for (int i = 0; i < symbols.length(); i++) // while not at end of line
        {

            search = "" + symbols.charAt(i); // add operator to search for

            if (line.contains(search)) // if operator found in line
            {
                line = line.replaceAll("\\W", " "); //remove symbol and replace with empty
                //used to be line = line.replaceALL(search, " ");

            }
        }

        strippedline=line;
        return strippedline;
    }

Thats my code so far, a few redundancies but im more or less still trying to figure out the best way to provide it lots of variant conditionals for the symbols, all this does is remove the symbols, i need it to remove for example both slashes // to the end of the line etc or both symbols /* to the next set in the same way a compiler might.

So yeh...best way to add conditionals to the remove statement above would be nice :), preferably in a light neat way such as a case statement or somehow similar.

Edited 3 Years Ago by trishtren

Perhasps use Regex instances to define each of the variants/replacements you are looking for?
You could place all the current Regex's in an ArrayList and loop thru them regardless of how nany there are.

This question has already been answered. Start a new discussion instead.