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);
else
return null;
}

Leave a Reply

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