Implementation of a substring operations in Java