javastringstring-length

Does Java's toLowerCase() preserve original string length?


Assume two Java String objects:

String str = "<my string>";
String strLower = str.toLowerCase();

Is it then true that for every value of <my string> the expression

str.length() == strLower.length()

evaluates to true?

So, does String.toLowerCase() preserve original string length for any value of String?


Solution

  • Surprisingly it does not!!

    From Java docs of toLowerCase

    Converts all of the characters in this String to lower case using the rules of the given Locale. Case mapping is based on the Unicode Standard version specified by the Character class. Since case mappings are not always 1:1 char mappings, the resulting String may be a different length than the original String.

    Example:

    package com.stackoverflow.q2357315;
    
    import java.util.Locale;
    
    public class Test {
        public static void main(String[] args) throws Exception {
            Locale.setDefault(new Locale("lt"));
            String s = "\u00cc";
            System.out.println(s + " (" + s.length() + ")"); // Ì (1)
            s = s.toLowerCase();
            System.out.println(s + " (" + s.length() + ")"); // i̇̀ (3)
        }
    }