How do I reverse a String in Dart?

2019-03-24 01:16发布

问题:

I have a String, and I would like to reverse it. For example, I am writing an AngularDart filter that reverses a string. It's just for demonstration purposes, but it made me wonder how I would reverse a string.

Example:

Hello, world

should turn into:

dlrow ,olleH

I should also consider strings with unicode characters. For example: 'Ame\u{301}lie'

What's an easy way to reverse a string, even if it has ?

回答1:

The question is not well defined. Reversing arbitrary strings does not make sense and will lead to broken output. The first (surmountable) obstacle is Utf-16. Dart strings are encoded as Utf-16 and reversing just the code-units leads to invalid strings:

var input = "Music \u{1d11e} for the win"; // Music               
                            
标签: dart