Package com.helger.base.string
Class StringScanner
java.lang.Object
com.helger.base.string.StringScanner
Simple string scanner.
- Author:
- Philip Helger
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintfindFirstIndex(char... aChars) Find the first index of any of the specified characters, starting from the current position.chargetCharAtIndex(int nIndex) Get the character at the specified absolute index.charintint@NonNull StringgetRest()Get all remaining chars, and set the index to the end of the input string@NonNull StringgetUntil(char cEndExcl) Get the string until the specified end character, but excluding the end character.@NonNull StringgetUntilBalanced(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 StringgetUntilIndex(int nEndIndex) Get the substring from the current position to the specified end index, and advance the current position to the end index.@NonNull StringGet all characters from the current position until the next whitespace character, and advance the position accordingly.booleanisCurrentChar(char c) Check if the current character matches the specified character.@NonNull StringScannersetIndex(int nIndex) Set the current scan position to the specified index.@NonNull StringScannerskip(int nCount) Skip the specified number of characters forward or backward.@NonNull StringScannerSkip all whitespace characters from the current position backward.@NonNull StringScannerSkip all whitespace characters from the current position forward.toString()
-
Constructor Details
-
StringScanner
Constructor.- Parameters:
sInput- The input string to scan. May not benull.
-
-
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
Skip all whitespace characters from the current position forward.- Returns:
- this for chaining.
-
skipbackWhitespaces
Skip all whitespace characters from the current position backward.- Returns:
- this for chaining.
-
skip
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:
trueif the current character equals the specified character.
-
setIndex
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
Get all remaining chars, and set the index to the end of the input string- Returns:
- The remaining string. May not be
nullbut may be empty.
-
getUntilIndex
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
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
Get the string until the specified end character, but excluding the end character.- Parameters:
cEndExcl- The end character to search.- Returns:
- A non-
nullstring with all characters from the current index until the end character, but not including the end character.
-
getUntilBalanced
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
-