You can use a case insensitive regular expression, for example...
String sentance = "Dang the dang and DANG and I don't mind a dANg";
sentance = sentance.replaceAll("(?i)dang", "#!");
System.out.println(sentance);
Which will output something like...
#! the #! and #! and I don't mind a #!
Updated based on comments
Without the ability to use replaceAll, you will have to split the String into sections, one way is to loop over the String, trimming off the "dang"s and building a new String from it, for example...
String sentance = "Dang the dang and DANG and I don't mind a dANg and some more";
StringBuilder sb = new StringBuilder(sentance.length());
while (sentance.toLowerCase().contains("dang")) {
int index = sentance.toLowerCase().indexOf("dang");
String start = sentance.substring(0, index);
int endIndex = index + "dang".length();
sb.append(start);
sb.append("#!");
sentance = sentance.substring(endIndex);
}
sb.append(sentance);
System.out.println(sb.toString());
Updated
You could use the case insensitive regular expression and String#split which will break the String into an array around the expression, you would then be able to rebuild the String from these parts...
String sentance = "Bang Dang the dang and DANG and I don't mind a dANg and some more";
String[] split = sentance.split("(?i)dang");
StringBuilder sb = new StringBuilder(sentance.length());
for (int index = 0; index < split.length - 1; index++) {
String part = split[index];
System.out.println("[" + part + "] " + part.trim().isEmpty());
if (!part.trim().isEmpty()) {
sb.append(part).append("#!");
} else {
sb.append("#!");
}
}
// Put the last value at the end, so we don't end up with #! at the end of the String
sb.append(split[split.length - 1]);
System.out.println(sb.toString());
I've not done any range checking (checking to see if there are enough parts returned), so you will need to do you own testing, but the ideas there...
"dang"to replace anything in"DANG Apple"?