Class SubstringIterator

  • All Implemented Interfaces:
    Iterator<String>


    public class SubstringIterator
    extends Object
    implements Iterator<String>
    This iterates over all possible substrings of a java.lang.String. For example: the string "abcde" will yield: "abcde", "abcd", "bcde", "abc", "bcd", "cde", "ab", "bc", "cd", "de", "a", "b", "c", "d", "e".

    There is also an option to return only unique substrings. If this option is active and the original string is "apple", then "p" will only be returned once instead of twice.

    • Constructor Detail

      • SubstringIterator

        public SubstringIterator(String text)
        Create a SubstringIterator that returns every possible substring of the original text.
        Parameters:
        text - the text to break into substrings.
      • SubstringIterator

        public SubstringIterator(String text,
                                 boolean onlyUniqueSubstrings)
        Create a SubstringIterator.
        Parameters:
        text - the text to break into substrings.
        onlyUniqueSubstrings - if this is true then this iterator will never return the same substring twice. For example substrings of "maintain" may include two copies of "ain" if this boolean is false.
    • Method Detail

      • hasNext

        public boolean hasNext()
      • getCurrentLength

        public int getCurrentLength()
        Returns the length of the substring to be returned by next(). This value decreases from the original string length to 1 as this iterator progresses.
      • remove

        public void remove()