Using groovy we will remove the first char in a string. We will use the phrase "groovy" and assert that each technique produces "roovy". First the getAt
accepts a range of numbers where we will pass the start and ending index. Second a standard substring
passing in a start index. Finally subsequence
which accepts a beginIndex and endIndex. A similar example shows how to remove the first char in a string using java.
getAt
@Test
void remove_first_char_with_getAt() {
def phrase = "groovy"
assert "roovy" == phrase.getAt(1..phrase.length() - 1)
//or equivalent shortcut
assert "roovy" == phrase[1..phrase.length() - 1]
}
Substring
@Test
void remove_first_char_with_substring() {
def phrase = "groovy"
assert "roovy" == phrase.substring(1)
}
subsequence
@Test
void remove_first_char_with_subsequence() {
def phrase = "groovy"
assert "roovy" == phrase.subSequence(1, phrase.length())
}