I want to check file name length
When i check the length of file name I got from mac is different from the length of the name I typed in IDE(Intellij).
So, I took the bytes of the string and printed it to the console, but it was different.
The case I want is the length as if i typed in IDE.
Is this possible?
How can i change the java bytes of string from macString to IDEString
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) {
String fileNameFromMac = "스크린샷.png";
String typedInIDE = "스크린샷.png";
System.out.println("File Name From Mac Length : " fileNameFromMac.length());
System.out.println("typedInIDE : " typedInIDE.length());
byte[] fileNameFromMacBytes = fileNameFromMac.getBytes(StandardCharsets.UTF_8);
byte[] typedInIDEBytes = typedInIDE.getBytes(StandardCharsets.UTF_8);
for(byte b : fileNameFromMacBytes) {
System.out.print(b);
}
System.out.println();
for(byte b : typedInIDEBytes) {
System.out.print(b);
}
}
}
CodePudding user response:
It's a difference in Unicode normalization. After normalizing to the same form, they will both have the same length (and the same bytes):
import java.text.Normalizer;
public class Main {
public static void main(String[] args) {
String fromMac = "스크린샷.png";
String fromIDE = "스크린샷.png";
System.out.println("From Mac length: " fromMac.length());
System.out.println("From IDE length: " fromIDE.length());
String normFromMac = Normalizer.normalize(fromMac, Normalizer.Form.NFD);
String normFromIDE = Normalizer.normalize(fromIDE, Normalizer.Form.NFD);
System.out.println("Normalized from Mac length: " normFromMac.length());
System.out.println("Normalized from IDE length: " normFromIDE.length());
}
}
This yields the following output:
From Mac length: 14
From IDE length: 8
Normalized from Mac length: 14
Normalized from IDE length: 14
See the Oracle Java Tutorial for more information.