Java: Concatenate a string before the last occurrence of any character

private String addToString(String source, char separator, String toBeInserted) {
int index = source.lastIndexOf(separator);
if(index >= 0 && index < source.length())
return source.substring(0, index) + toBeInserted + source.substring(index);
return null;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.