Skip to main content
deleted 23 characters in body
Source Link
OldCurmudgeon
  • 2.1k
  • 3
  • 19
  • 30

public class Test {

public class Test {

added 709 characters in body
Source Link
OldCurmudgeon
  • 2.1k
  • 3
  • 19
  • 30

public class Test {

public class Test {

    /**
     * Use a Stripper to encode the stripping process.
     */
    private static class Stripper {

        /**
         * Boolean lookup for maximimum performance.
         *
         * Could use BitSet to save a little space.
         *
         * Length 256 - assumes ASCII.
         */
        private final boolean[] strip = new boolean[256];

        // Compiles the strip string.
        public Stripper(CharSequence strip) {
            for (int i = 0; i < strip.length(); i++) {
                char ch = strip.charAt(i);
                // Both lowercase and uppercase.
                this.strip[Character.toLowerCase(ch)] = true;
                this.strip[Character.toUpperCase(ch)] = true;
            }
        }

        /**
         * Strips all characters from s that were in the `strip` parameter of
         * the constructor.
     *    *
         * Note the use of `CharSequence` to avoid premature `toString`.
     *    *
         * @param s - The source of the data to strip.
         * @return The stripped version of s
         */
        public CharSequence strip(CharSequence s) {
            StringBuilder stripped = new StringBuilder();
            for (int i = 0; i < s.length(); i++) {
                char ch = s.charAt(i);
                if (!strip[ch]) {
                    stripped.append(ch);
                }
            }
            return stripped;
        }
    }

    public static final String strip(String from, String strip) {
        return new Stripper(strip).strip(from).toString();
    }

    public void test() {
        System.out.println("Stripped: " + strip("John Lennon", "Paul ON"));

    }

    public static void main(String args[]) {
        try {
            new Test().test();
        } catch (Throwable ex) {
            ex.printStackTrace(System.err);
        }
    }
}

Please forgive the I'd do it a completely different way - this answer was posted before it was moved to Code Review.

Added

It seems a bit of narrative would be useful.

Obviously the desired process comes in two parts, first the setup and second the stripping of characters from the original string. This code performs the setup during the construction of the object by building an array of 256 booleans, one for each possible 8-bit character code. Each boolean holds a true value if the character at this position should be stripped from the string.

This ensures that the absolute minimum needs to be done at process time. All that is required is to take one pass through the string, looking up each character in the boolean array. If we find true then the character is discarded, a false means keep it.

/**
 * Use a Stripper to encode the stripping process.
 */
private static class Stripper {

    /**
     * Boolean lookup for maximimum performance.
     *
     * Could use BitSet to save a little space.
     *
     * Length 256 - assumes ASCII.
     */
    private final boolean[] strip = new boolean[256];

    // Compiles the strip string.
    public Stripper(CharSequence strip) {
        for (int i = 0; i < strip.length(); i++) {
            char ch = strip.charAt(i);
            // Both lowercase and uppercase.
            this.strip[Character.toLowerCase(ch)] = true;
            this.strip[Character.toUpperCase(ch)] = true;
        }
    }

    /**
     * Strips all characters from s that were in the `strip` parameter of the constructor.
     * 
     * Note the use of `CharSequence` to avoid premature `toString`.
     * 
     * @param s - The source of the data to strip.
     * @return The stripped version of s
     */
    public CharSequence strip(CharSequence s) {
        StringBuilder stripped = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (!strip[ch]) {
                stripped.append(ch);
            }
        }
        return stripped;
    }
}

public static final String strip(String from, String strip) {
    return new Stripper(strip).strip(from).toString();
}

public void test() {
    System.out.println("Stripped: " + strip("John Lennon", "Paul ON"));

}

Please forgive the I'd do it a completely different way - this answer was posted before it was moved to Code Review.

public class Test {

public class Test {

    /**
     * Use a Stripper to encode the stripping process.
     */
    private static class Stripper {

        /**
         * Boolean lookup for maximimum performance.
         *
         * Could use BitSet to save a little space.
         *
         * Length 256 - assumes ASCII.
         */
        private final boolean[] strip = new boolean[256];

        // Compiles the strip string.
        public Stripper(CharSequence strip) {
            for (int i = 0; i < strip.length(); i++) {
                char ch = strip.charAt(i);
                // Both lowercase and uppercase.
                this.strip[Character.toLowerCase(ch)] = true;
                this.strip[Character.toUpperCase(ch)] = true;
            }
        }

        /**
         * Strips all characters from s that were in the `strip` parameter of
         * the constructor.
         *
         * Note the use of `CharSequence` to avoid premature `toString`.
         *
         * @param s - The source of the data to strip.
         * @return The stripped version of s
         */
        public CharSequence strip(CharSequence s) {
            StringBuilder stripped = new StringBuilder();
            for (int i = 0; i < s.length(); i++) {
                char ch = s.charAt(i);
                if (!strip[ch]) {
                    stripped.append(ch);
                }
            }
            return stripped;
        }
    }

    public static final String strip(String from, String strip) {
        return new Stripper(strip).strip(from).toString();
    }

    public void test() {
        System.out.println("Stripped: " + strip("John Lennon", "Paul ON"));

    }

    public static void main(String args[]) {
        try {
            new Test().test();
        } catch (Throwable ex) {
            ex.printStackTrace(System.err);
        }
    }
}

Please forgive the I'd do it a completely different way - this answer was posted before it was moved to Code Review.

Added

It seems a bit of narrative would be useful.

Obviously the desired process comes in two parts, first the setup and second the stripping of characters from the original string. This code performs the setup during the construction of the object by building an array of 256 booleans, one for each possible 8-bit character code. Each boolean holds a true value if the character at this position should be stripped from the string.

This ensures that the absolute minimum needs to be done at process time. All that is required is to take one pass through the string, looking up each character in the boolean array. If we find true then the character is discarded, a false means keep it.

added 2 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Jhe

Jhe

This algorithm should have an O(n)\$O(n)\$ construction complexity and an O(n)\$O(n)\$ processing complexity.

Please forgive the i'd do it a completely different wayI'd do it a completely different way - this answer was posted before it was moved to Code Review.

Jhe

This algorithm should have an O(n) construction complexity and an O(n) processing complexity.

Please forgive the i'd do it a completely different way - this answer was posted before it was moved to Code Review.

Jhe

This algorithm should have an \$O(n)\$ construction complexity and an \$O(n)\$ processing complexity.

Please forgive the I'd do it a completely different way - this answer was posted before it was moved to Code Review.

added 6 characters in body
Source Link
OldCurmudgeon
  • 2.1k
  • 3
  • 19
  • 30
Loading
added 122 characters in body
Source Link
OldCurmudgeon
  • 2.1k
  • 3
  • 19
  • 30
Loading
Post Migrated Here from stackoverflow.com (revisions)
Source Link
OldCurmudgeon
  • 2.1k
  • 3
  • 19
  • 30
Loading