How do I reverse a String in Dart?

前端 未结 7 744
面向向阳花
面向向阳花 2021-02-03 20:13

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 wo

7条回答
  •  梦如初夏
    2021-02-03 20:59

    Here's one way to reverse an ASCII String in Dart:

    input.split('').reversed.join('');
    
    1. split the string on every character, creating an List
    2. generate an iterator that reverses a list
    3. join the list (creating a new string)

    Note: this is not necessarily the fastest way to reverse a string. See other answers for alternatives.

    Note: this does not properly handle all unicode strings.

提交回复
热议问题