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.