4 ms·
For anyone else who was curious C#/.NET: By default string.Length measures characters, but System.Globalization.StringInfo is provided to work with "text eleme
by eterm 1y ago
For anyone else who was curious C#/.NET:
By default string.Length measures characters, but System.Globalization.StringInfo is provided to work with "text elements".
Unlike System.String, StringInfo doesn't have a built-in PadLeft function in it's base library. But it gets the length "right" by the author's standard.
Code to show lengths:
using System;
using System.Globalization;
public class Program
{
public static void Main()
{
var weirdStrings = new string[] {"𝄞","Å","𓀾","אֳֽ֑","résumé","résumé"};
foreach(var weirdString in weirdStrings){
var asStringInfo = new StringInfo(weirdString);
Console.WriteLine($"{weirdString.PadLeft(10,'-')} {weirdString.Length} {asStringInfo.LengthInTextElements}");
}
}
}