-2

I am trying to make a program that censors the word "dang" with a "#!". My main method passes in a String sentence.

public static String deleteDang(String sentence)
 /* pre: receives the sentence to be censored
 post: returns the censored sentence*/
 {
 while(((sentence.toLowerCase()).contains("dang")))
 {
 sentence = (sentence.toLowerCase()).replace("dang", "#!");
 }
 return sentence;
 }

I want it to be work so that I can enter something such as "DANG ApplE" and get the output "!# ApplE". When I attempt to run this, it outputs "!# apple" if I input "dang apple", but when I put in "DANG Apple", I have an infinite output of "DANG Apple". What am I doing wrong?

If possible, I would like to this without the use of .replaceAll

asked Oct 17, 2014 at 3:11
2
  • I don't see you outputting anything. Where does this happen? Commented Oct 17, 2014 at 3:14
  • Also, why would you expect "dang" to replace anything in "DANG Apple"? Commented Oct 17, 2014 at 3:16

2 Answers 2

4

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...

answered Oct 17, 2014 at 3:16
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way that I could do it by iterating? I am trying to understand this for a class and my teacher doesn't want us to use .replaceAll yet.
Thanks a ton!The first update is really helpful and now I understand.
0

You don't have to iterate. You can just use replaceAll method

sentence = sentence.replaceAll("dang", "#!");

This will only consider the exact word "dang"

If you want to replace all dang word without considering the case then you can try

 sentence = sentence.replaceAll("(?!)dang", "#!");
answered Oct 17, 2014 at 3:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.