Replace and replaceall methods of the string object in JDK

Source: Internet
Author: User
Java, as the most popular and popular language for developers, shows its features in many aspects. Although it is no more flexible than C ++, its execution efficiency is no better than C, and its development efficiency is no better than Ruby, but as the earliest and most pure OO language, Java is widely used in programming because of its continuity with C and C ++ languages. Even if Java is so popular, I personally think that JDK APIs are not doing very well in many aspects. Simply put, it is named. For example, the two methods of the string class replace and replaceall, because replaceall exists, it is easy to think that the first replace method is to replace the first target string in the source string with the replacement string, the replaceall method replaces all target strings in the source string.
At least in JDK, they are declared as follows: Public String replaceall (string RegEx, string replacement ){...}

Public String Replace (charsequence target, charsequence replacement ){...}
If you do not read their comments or view specific implementations, you naturally think that there is only a difference between the two replicas. One is to replicas all, the other one is to replace the first one (at least I understand this, and I have suffered some losses on this API ). Replaceall replaces each substring of this string that matches the given
Regular Expression with the given replacement. replacereplaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. the replacement proceeds from the beginning of the string to the end, for example, replacing "AA" with "B" in the string "AAA" will result in "ba" rather than "AB ". so far, we know that replaceall uses a regular expression to replace text and replaces all substrings that meet the conditions. Replace uses the content of the text for matching and replacement, and is also a substring that meets the conditions.
For example, the following code:
                String info = "catfish+foolishcat";String withReplace = info.replace("catfish+", "rxr");String withReplaceAll = info.replaceAll("catfish+", "rxr");System.out.println("withReplace    is " + withReplace);System.out.println("withReplaceAll is " + withReplaceAll);

The final output is as follows:

Withreplace is rxrfoolishcat
Withreplaceall is RXR + foolishcat. Therefore, these two methods give developers too much ambiguity in naming and are not readable. At least they cannot be used properly. Replace with replacewithliteraltarget, replaceall with replacewithregextarget can distinguish the difference between them from the semantics. There are so many naming conventions for replace and replaceall. We have time to record some of the define statements that I think are not good in JDK to avoid making the same mistake in the future.

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.