I am converting the following C# code to Java. Is there a Java equivalent to the .NET concept of Invariant Culture?
string upper = myString.ToUpperInvariant();
Since the Invariant Culture is really just the US culture, I could just do something like this in Java, but I’m wondering if there is a better way:
String upper = myString.toUpperCase(Locale.US);
Update: Java 6 introduced
Locale.ROOTwhich is described as:This is probably better than using US, but I haven’t checked it against the code below.
No, that’s basically the right way to go. While there are differences between the US culture and the invariant culture in terms of formatting, I don’t believe they affect casing rules.
EDIT: Actually, a quick test program shows there are characters which are upper-cased differently in .NET in the US culture to in the invariant culture:
Output:
I don’t have time to look at these right now, but it’s worth investigating. I don’t know if the same differences would apply in Java – you probably want to take a sample of them and work out what you want your code to do.
EDIT: And just to be completist, it’s worth mentioning that that only checks for individual characters… whereas you’re really upper-casing whole strings, which can make a difference.
Looking at the Java code for upper-casing, that appears to only have locale-specific behaviour for tr, az and lt countries. I know that tr is Turkey, but I don’t know about the others…