Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do I replace several types of text in Java in one string?

I'm pretty new to Java, I've only been learning it for a month so don't judge me.

Basically, I'm just trying to figure out how to replace more than one thing in a string like the title says. Here's my code:

public class ReplaceDemo {

public static void main(String[] args) {

String str = "well, i am certainly typing this sentence badly. capitalization is important.";

String strreplace = " I ";

String strreplace2 = " C";

String result = str.replaceAll(" i " , strreplace);

String result2 = str.replaceAll(" c" , strreplace2);

System.out.println(result);

System.out.println(result2);

}

}

Obviously that doesn't work. This is the output:

well, I am certainly typing this sentence badly. capitalization is important.

well, i am Certainly typing this sentence badly. Capitalization is important.

Yeah, I also realize that just replaces every 'c' with 'C', but that isn't really important right now. If you want you can tell me how to fix that, but I just wanna know how to change more than one thing in a string like I said.

1 Answer

Relevance
  • ?
    Lv 4
    9 years ago
    Favorite Answer

    Your methodology is okay for now. I would advise you spend some time looking into regular expressions. They are very powerful for string manipulation. For your example I have only a couple changes.

    1. A capitalization should only occur after a period, proper noun (your not considering this case), or the pronoun I. Thus replace ". c" with ". C". Since the replaceAll method takes a regex, a period is denoted as "\." and in a java string "\" is reserved for escape characters, so "\\" indicates a single backslash when interpreted.

    String strreplace2 = "\\. C";

    2. For both replacements to occur on the string you have to perform them on the same object one after the other. The String.replace, String.replaceAll, and String.replaceFirst return a _new_ string with the replacements. It does not alter the original object. That is why if you print out the original string "str" it will be the same.

    Currently you are replacing all lowercase i's with capital I's and storing it in result. Then you are replacing all lowercase c's with capital C's on the original string and storing it in result2, a different string object. You must make your replacement for lowercase c on the string where the i's are already replaced.

    String strreplace = " I ";

    String strreplace2 = "\\. C";

    String result = str.replaceAll(" i " , strreplace);

    String result2 = result.replaceAll("\\. c", strreplace2);

    System.out.println(result);

    System.out.println(result2);

    //Output

    well, I am certainly typing this sentence badly. capitalization is important.

    well, I am certainly typing this sentence badly. Capitalization is important.

Still have questions? Get your answers by asking now.