What is the difference between Character.toUpperCase () and Character.toTitleCase ()

I was reviewing some of my old code, and then found out that I used the Character.toTitleCase() method at some point and could not help but wonder if Character.toUpperCase() better.

I read their descriptions and did not see a fundamental difference:

toUpperCase

Converts a character argument to uppercase using case mapping information from a UnicodeData file. Note that Character.isUpperCase (Character.toUpperCase (ch)) does not always return true for some character ranges, especially those that are characters or ideograms.

In general, String.toUpperCase () should be used to map characters to uppercase. String matching methods have several advantages. Character mapping methods. String mapping methods can perform locally sensitive mappings, context-sensitive mappings, and 1: M character mappings, while random character mapping methods cannot.

Note. This method cannot handle extra characters. To support all Unicode characters, including additional characters, use toUpperCase (int).

and

toTitleCase

Converts a character argument to a header file using case mapping information from a UnicodeData file. If the character does not have an explicit header mapping and is not itself a char header in accordance with UnicodeData, then the uppercase mapping is returned as the equivalent of the header mapping. If the char argument is already a char header, then the char value will be returned. Note that Character.isTitleCase (Character.toTitleCase (ch)) does not always return true for some character ranges.

Note. This method cannot handle extra characters. Support all Unicode characters, including extra characters, use toTitleCase (int).

Then I tried to check them as follows:

 public class Test { public static void main(String... args) { String originalString = "abcdefghijklmnopqrstuvwxyz123546-.,/*&%+"; StringBuilder upperCaseStringBuilder = new StringBuilder(); StringBuilder titleCaseStringBuilder = new StringBuilder(); for (int i = 0; i < originalString.length(); i++) { upperCaseStringBuilder.append(Character.toUpperCase(originalString.charAt(i))); titleCaseStringBuilder.append(Character.toTitleCase(originalString.charAt(i))); } System.out.println("Original String : " + originalString); System.out.println("UpperCase result: " + upperCaseStringBuilder.toString()); System.out.println("TitleCase result: " + titleCaseStringBuilder.toString()); } } 

This is the conclusion:

 Original String : abcdefghijklmnopqrstuvwxyz123546-.,/*&%+ UpperCase result: ABCDEFGHIJKLMNOPQRSTUVWXYZ123546-.,/*&%+ TitleCase result: ABCDEFGHIJKLMNOPQRSTUVWXYZ123546-.,/*&%+ 

Therefore, I could not understand the difference between these two methods. As I said, I used toTitleCase() in my code to use the String value.

Is there any key difference that I have not considered and which could lead to my code behaving differently than expected in some special cases?


Note I don't think this duplicate String capitalize is the best way . Because in this question, the problem is related to the performance of string caching, and not with the upper and title characters of characters, as in this question.

+5
source share
2 answers

Standard ASCII characters are so boring! Here's something more exciting :

 System.out.println(Character.toTitleCase('dz')); // Dz System.out.println(Character.toUpperCase('dz')); // DZ 

Live demo.

+2
source

I really don't see any “real” differences here, and I think it's best not to overdo it with a little detail. I want to point to this single line here, which should help with the "problem" here.

In general, String.toUpperCase() should be used to match uppercase characters. String matching methods have several advantages over character mapping methods.

Documentation String.toUpperCase() : https://www.w3schools.com/jsref/jsref_touppercase.asp

Someone should comment if they have more inside information.

-2
source

Source: https://habr.com/ru/post/1274193/


All Articles