How do I reverse a String in Dart?

前端 未结 7 741
面向向阳花
面向向阳花 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:39

    There are many ways to reverse a string in Dart, some of them are given below.

    Use split and join:

    String reverseStringUsingSplit(String input) {
      var chars = input.split('');
      return chars.reversed.join();
    }
    

    

Use runes:

    String reverseStringUsingRunes(String input) {
      var chars = input.runes.toList();
    
      return String.fromCharCodes(chars.reversed);
    }
    

    

Use codeUnits:

    String reverseStringUsingCodeUnits(String input) {
      return String.fromCharCodes(input.codeUnits.reversed);
    }
    

    You can simply use all methods like this

    void main() {
      var coflutter = 'Coflutter';
    
      print(reverseStringUsingSplit(coflutter));
      print(reverseStringUsingRunes(coflutter));
      print(reverseStringUsingCodeUnits(coflutter));
    }
    

    The output of all 3 methods will be

    rettulfoC
    rettulfoC
    rettulfoC
    

    Ref: https://coflutter.com/dart-how-to-reverse-a-string/

    0 讨论(0)
  • 2021-02-03 20:48

    Try this function

    String reverse(String s) {
      var chars = s.splitChars();
      var len   = s.length - 1;
      var i     = 0;
    
      while (i < len) {
        var tmp = chars[i];
        chars[i] = chars[len];
        chars[len] = tmp;
        i++;
        len--;
      }
    
      return Strings.concatAll(chars);
    }
    
    void main() {
      var s = "Hello , world";
      print(s);
      print(reverse(s));
    }
    

    (or)

    String reverse(String s) {
      StringBuffer sb=new StringBuffer();
      for(int i=s.length-1;i>=0;i--) {
        sb.add(s[i]);
      }
      return sb.toString();
    }
    
    main() {
      print(reverse('Hello , world'));
    }
    
    0 讨论(0)
  • 2021-02-03 20:48

    There is a utils package that covers this function. It has some more nice methods for operation on strings.

    Install it with :

    dependencies:
      basic_utils: ^1.2.0
    

    Usage :

    String reversed = StringUtils.reverse("helloworld");
    

    Github:

    https://github.com/Ephenodrom/Dart-Basic-Utils

    0 讨论(0)
  • 2021-02-03 20:51

    The library More Dart contains a light-weight wrapper around strings that makes them behave like an immutable list of characters:

    import 'package:more/iterable.dart';
    
    void main() {
      print(string('Hello World').reversed.join());
    }
    
    0 讨论(0)
  • 2021-02-03 20:54

    I've made a small benchmark for a few different alternatives:

    String reverse0(String s) {
      return s.split('').reversed.join('');
    }
    
    String reverse1(String s) {
      var sb = new StringBuffer();
      for(var i = s.length - 1; i >= 0; --i) {
        sb.write(s[i]);
      }
      return sb.toString();
    }
    
    String reverse2(String s) {
      return new String.fromCharCodes(s.codeUnits.reversed);
    }
    
    String reverse3(String s) {
      var sb = new StringBuffer();
      for(var i = s.length - 1; i >= 0; --i) {
        sb.writeCharCode(s.codeUnitAt(i));
      }
      return sb.toString();
    }
    
    String reverse4(String s) {
      var sb = new StringBuffer();
    
      var i = s.length - 1;
    
      while (i >= 3) {
        sb.writeCharCode(s.codeUnitAt(i-0));
        sb.writeCharCode(s.codeUnitAt(i-1));
        sb.writeCharCode(s.codeUnitAt(i-2));
        sb.writeCharCode(s.codeUnitAt(i-3));
        i -= 4;
      }
    
      while (i >= 0) {
        sb.writeCharCode(s.codeUnitAt(i));
        i -= 1;
      }
    
      return sb.toString();
    }
    
    String reverse5(String s) {
      var length = s.length;
      var charCodes = new List(length);
      for(var index = 0; index < length; index++) {
        charCodes[index] = s.codeUnitAt(length - index - 1);
      }
    
      return new String.fromCharCodes(charCodes);
    }
    main() {
      var s = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
    
      time('reverse0', () => reverse0(s));
      time('reverse1', () => reverse1(s));
      time('reverse2', () => reverse2(s));
      time('reverse3', () => reverse3(s));
      time('reverse4', () => reverse4(s));
      time('reverse5', () => reverse5(s));
    }
    

    Here is the result:

    reverse0: => 331,394 ops/sec (3 us) stdev(0.01363)
    reverse1: => 346,822 ops/sec (3 us) stdev(0.00885)
    reverse2: => 490,821 ops/sec (2 us) stdev(0.0338)
    reverse3: => 873,636 ops/sec (1 us) stdev(0.03972)
    reverse4: => 893,953 ops/sec (1 us) stdev(0.04089)
    reverse5: => 2,624,282 ops/sec (0 us) stdev(0.11828)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题