Remove first character from string

The opposite of deleting the last character in a string this example will show how to delete the first character in a java string.

Straight up Java

Using straight up java we will check if the phrase is null and if the length is greater than 1. If the length is less than 1 then we know the String is empty. Calling String.substring we will pass 1 as the starting index and the total length of the phrase as the end index. Since it will read the string from a 0 base index it will delete the first char from the String. You can remove any number of characters from the beginning position just remember to update the if statement.

@Test
public void delete_first_char_java() {

    String phrase = "TRON: Legacy";

    String rephrase = null;
    if (phrase != null && phrase.length() > 1) {
        rephrase = phrase.substring(1, phrase.length());
    }

    assertEquals("RON: Legacy", rephrase);
}

Apache Commons

Using apache commons StringUtils we will call the right method, which will get the right most characters of a String. If we subtract one from the total length of the string the statement will return a string without the first character.

@Test
public void delete_last_char_apache() {

    String phrase = "TRON: Legacy";

    String rephrase = StringUtils.right(phrase, phrase.length() - 1);

    assertEquals("RON: Legacy", rephrase);
}