Class StringLocation


  • public class StringLocation
    extends Object
    Represents a location in a String.

    This basically has three properties, position, line and column. Position and column are counted based on Unicode code units, which means that a surrogate pair uses two positions (columns).

    Position starts from zero, while line and column start from one.

    A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

    Assume this text:

      abc\n def\r\n ghi\r jkl\n\r mn
     

    Then, the position, line and column of each character are as shown below.

    • 'a': position = 0, line = 1, column = 1
    • 'b': position = 1, line = 1, column = 2
    • 'c': position = 2, line = 1, column = 3
    • \n : position = 3, line = 1, column = 4
    • ' ': position = 4, line = 2, column = 1
    • 'd': position = 5, line = 2, column = 2
    • 'e': position = 6, line = 2, column = 3
    • 'f': position = 7, line = 2, column = 4
    • \r : position = 8, line = 2, column = 5
    • \n : position = 9, line = 2, column = 6
    • ' ': position = 10, line = 3, column = 1
    • 'g': position = 11, line = 3, column = 2
    • 'h': position = 12, line = 3, column = 3
    • 'i': position = 13, line = 3, column = 4
    • \r : position = 14, line = 3, column = 5
    • ' ': position = 15, line = 4, column = 1
    • 'j': position = 16, line = 4, column = 1
    • 'k': position = 17, line = 4, column = 2
    • 'l': position = 18, line = 4, column = 3
    • \n : position = 19, line = 4, column = 4
    • \r : position = 20, line = 5, column = 1
    • ' ': position = 21, line = 6, column = 1
    • 'm': position = 22, line = 6, column = 2
    • 'n': position = 23, line = 6, column = 3

    Instances of this class are immutable.

    • Method Detail

      • position

        public int position()
        Returns:
        position in a certain String starting from zero
      • line

        public int line()
        Returns:
        line number in a certain String starting from one.
      • column

        public int column()
        Returns:
        column number in a certain String starting from one.
      • previous

        @Nullable
        public @Nullable StringLocation previous()
        Returns:
        the instance of StringLocation which represents the previous location of this instance.
      • next

        public StringLocation next​(boolean newline)
        Parameters:
        newline - true if the next character is a new line
        Returns:
        an instance of StringLocation which represents the next location of this instance.