0

If I have an arbitrary String like this:

final family = '\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}'; // ‍‍
final myString = 'Let me introduce my $family to you.';

And I know the String index of the character after the family emoji (the space) is 28, how do I find the String index of the first code unit of the family emoji? In other words, how to I find the length in UTF-16 code units of the family emoji?

enter image description here

I've asked a similar question before, but that was before the characters package came out. Is there anything I can do to map the grapheme cluster index to the String index?

The reason I'm asking is that I want to programmatically backspace by grapheme cluster inside a Flutter TextField.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • Isn't `family.length` is the length of `family` in UTF-16 code units? – jamesdlin Dec 25 '20 at 04:30
  • @jamesdlin, Yes, I wrote it like that to make the string more readable, but I guess that was misleading. What I'd like know is how to find the length for some arbitrary unknown grapheme cluster. – Suragch Dec 25 '20 at 04:41

1 Answers1

0

It appears that String.characters gives you a list of one grapheme cluster per element. Just remove the stuff a character at a time using a range as described in the docs (https://pub.dev/documentation/characters/latest/characters/CharacterRange-class.html).

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70