You should use already tested and well documented libraries in favor of writing your own code!
StringUtils.replaceOnce("aba", "a", "") = "ba"
The StringUtils
class is from Apache Commons Lang3 package and can be imported in Maven like this:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>