Java ReplaceAll is case insensitive

Source: Internet
Author: User

How does ReplaceAll ignore capitalization in Java?

Method One: Add in front of the regular expression (? i)

Java code
    1. @Test   
    2.     public void test_replaceall33 () {  
    3.         string input =  "I  like java,java is very easy and java is so popular. ";   
    4.         string replacement=
    5.   
    6.          SYSTEM.OUT.PRINTLN (input);   
    7.          System.out.println (Input.replaceall (
    8.     }  
@Testpublic void Test_replaceall33 () {String input = "I like Java,java are very easy and Java are so popular."; String replacement= "CCCC"; SYSTEM.OUT.PRINTLN (input); System.out.println (Input.replaceall ("(? i) Java", replacement));}

Test results:

Mode two: Using the Appendreplacement method of Matcher

Java code
  1. @Test
  2. public void Test_replaceall () {
  3. String input = "I like Java,java are very easy and Java are so popular.";
  4. String regex = "Java";
  5. String replacement="CCCC";
  6. StringBuffer SB =replaceall2 (input, regex, replacement);
  7. SYSTEM.OUT.PRINTLN (input);
  8. System.out.println (SB);
  9. }
  10. /*** 
  11. * ReplaceAll, ignoring case
  12. * @param input
  13. * @param regex
  14. * @param replacement
  15. * @return
  16. */
  17. Public StringBuffer replaceAll2 (String input,string regex,string replacement) {
  18. Pattern p = pattern.compile (regex,pattern.case_insensitive);
  19. Matcher m = p.matcher (input);
  20. StringBuffer sb = new StringBuffer ();
  21. Boolean result = M.find ();
  22. While (result)
  23. {
  24. M.appendreplacement (SB, replacement);
  25. result = M.find ();
  26. }
  27. M.appendtail (SB);
  28. return SB;
  29. }
@Testpublic void Test_replaceall () {String input = "I like Java,java are very easy and Java are so popular.";        String regex = "Java";        String replacement= "CCCC";                StringBuffer SB =replaceall2 (input, regex, replacement);        SYSTEM.OUT.PRINTLN (input);        System.out.println (SB);} /*** * ReplaceAll, ignoring case * @param input * @param regex * @param replacement * @return */public stringbuffer replaceAll2 (Str ing input,string regex,string replacement) {Pattern p = pattern.compile (regex,pattern.case_insensitive);        Matcher m = p.matcher (input);        StringBuffer sb = new StringBuffer ();        Boolean result = M.find ();        while (result)        {            m.appendreplacement (SB, replacement);            result = M.find ();        }        M.appendtail (SB);        return SB;}

Test results:

Method Three: Use Matcher's ReplaceAll

Java code
  1. /***
  2. * ReplaceAll, ignoring case
  3. *
  4. * @param input
  5. * @param regex
  6. * @param replacement
  7. * @return
  8. */
  9. Public string ReplaceAll3 (String input, string regex, string replacement) {
  10. Pattern p = pattern.compile (regex, pattern.case_insensitive);
  11. Matcher m = p.matcher (input);
  12. String result = M.replaceall (replacement);
  13. return result;
  14. }
  15. @Test
  16. public void Test_replaceall3 () {
  17. String input = "I like Java,java are very easy and Java are so popular.";
  18. String regex = "Java";
  19. String replacement = "CCCC";
  20. String sb = ReplaceAll3 (input, regex, replacement);
  21. SYSTEM.OUT.PRINTLN (input);
  22. System.out.println (SB);
  23. }
/*** * ReplaceAll, ignoring case *  * @param input * @param regex * @param replacement * @return */public String replaceAll3 (Str ing input, string regex, string replacement) {Pattern p = pattern.compile (regex, pattern.case_insensitive); Matcher m = p.matcher (input); String result = M.replaceall (replacement); return result;} @Testpublic void Test_replaceall3 () {String input = "I like Java,java are very easy and Java are so popular."; String regex = "Java"; String replacement = "CCCC"; String sb = ReplaceAll3 (input, regex, replacement); SYSTEM.OUT.PRINTLN (input); System.out.println (SB);}

Test results: Reference: http://hw1287789687.iteye.com/blog/2150892

Java ReplaceAll is case insensitive

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.