Class StringScanner

java.lang.Object
com.helger.base.string.StringScanner

@NotThreadSafe public class StringScanner extends Object
Simple string scanner.
Author:
Philip Helger
  • Constructor Summary

    Constructors
    Constructor
    Description
    StringScanner(@NonNull String sInput)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    findFirstIndex(char... aChars)
    Find the first index of any of the specified characters, starting from the current position.
    char
    getCharAtIndex(int nIndex)
    Get the character at the specified absolute index.
    char
     
    int
     
    int
     
    @NonNull String
    Get all remaining chars, and set the index to the end of the input string
    @NonNull String
    getUntil(char cEndExcl)
    Get the string until the specified end character, but excluding the end character.
    @NonNull String
    getUntilBalanced(int nStartLevel, char cOpenChar, char cCloseChar)
    Get all characters from the current position until the bracket nesting level reaches zero, and advance the position accordingly.
    @NonNull String
    getUntilIndex(int nEndIndex)
    Get the substring from the current position to the specified end index, and advance the current position to the end index.
    @NonNull String
    Get all characters from the current position until the next whitespace character, and advance the position accordingly.
    boolean
    isCurrentChar(char c)
    Check if the current character matches the specified character.
    @NonNull StringScanner
    setIndex(int nIndex)
    Set the current scan position to the specified index.
    @NonNull StringScanner
    skip(int nCount)
    Skip the specified number of characters forward or backward.
    @NonNull StringScanner
    Skip all whitespace characters from the current position backward.
    @NonNull StringScanner
    Skip all whitespace characters from the current position forward.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • StringScanner

      public StringScanner(@NonNull String sInput)
      Constructor.
      Parameters:
      sInput - The input string to scan. May not be null.
  • Method Details

    • getCurrentIndex

      @Nonnegative public int getCurrentIndex()
      Returns:
      The current scan position index. Always ≥ 0.
    • getRemainingChars

      @Nonnegative public int getRemainingChars()
      Returns:
      The number of remaining characters from the current position to the end. Always ≥ 0.
    • skipWhitespaces

      public @NonNull StringScanner skipWhitespaces()
      Skip all whitespace characters from the current position forward.
      Returns:
      this for chaining.
    • skipbackWhitespaces

      public @NonNull StringScanner skipbackWhitespaces()
      Skip all whitespace characters from the current position backward.
      Returns:
      this for chaining.
    • skip

      public @NonNull StringScanner skip(int nCount)
      Skip the specified number of characters forward or backward.
      Parameters:
      nCount - The number of characters to skip. May be negative to go backward.
      Returns:
      this for chaining.
    • findFirstIndex

      public int findFirstIndex(char... aChars)
      Find the first index of any of the specified characters, starting from the current position.
      Parameters:
      aChars - The characters to search for.
      Returns:
      The index of the first match, or -1 if none of the characters was found.
    • getCharAtIndex

      public char getCharAtIndex(int nIndex)
      Get the character at the specified absolute index.
      Parameters:
      nIndex - The absolute index in the input string.
      Returns:
      The character at the specified index.
      Throws:
      IllegalArgumentException - if the index is out of bounds.
    • getCurrentChar

      public char getCurrentChar()
      Returns:
      The character at the current scan position.
    • isCurrentChar

      public boolean isCurrentChar(char c)
      Check if the current character matches the specified character.
      Parameters:
      c - The character to compare against.
      Returns:
      true if the current character equals the specified character.
    • setIndex

      public @NonNull StringScanner setIndex(@Nonnegative int nIndex)
      Set the current scan position to the specified index.
      Parameters:
      nIndex - The new index. Must be between 0 and the length of the input string (inclusive).
      Returns:
      this for chaining.
    • getRest

      public @NonNull String getRest()
      Get all remaining chars, and set the index to the end of the input string
      Returns:
      The remaining string. May not be null but may be empty.
    • getUntilIndex

      public @NonNull String getUntilIndex(int nEndIndex)
      Get the substring from the current position to the specified end index, and advance the current position to the end index.
      Parameters:
      nEndIndex - The end index (exclusive).
      Returns:
      The substring. Never null.
    • getUntilWhiteSpace

      public @NonNull String getUntilWhiteSpace()
      Get all characters from the current position until the next whitespace character, and advance the position accordingly.
      Returns:
      The non-whitespace substring. Never null.
    • getUntil

      public @NonNull String getUntil(char cEndExcl)
      Get the string until the specified end character, but excluding the end character.
      Parameters:
      cEndExcl - The end character to search.
      Returns:
      A non-null string with all characters from the current index until the end character, but not including the end character.
    • getUntilBalanced

      public @NonNull String getUntilBalanced(int nStartLevel, char cOpenChar, char cCloseChar)
      Get all characters from the current position until the bracket nesting level reaches zero, and advance the position accordingly.
      Parameters:
      nStartLevel - The initial nesting level.
      cOpenChar - The opening bracket character that increases the nesting level.
      cCloseChar - The closing bracket character that decreases the nesting level.
      Returns:
      The content between the balanced brackets. Never null.
    • toString

      public String toString()
      Overrides:
      toString in class Object