Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

flutter - How to convert String index to character index in Dart

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...