DART:如何随机重新排列字符串中的字符?

DART: how can i reorder randomly characters in a string?

如何随机更改字符串中的字符顺序?

示例,输入:hello - 输出:elolh

您可以使用 List.shuffle :

var text = 'HELLO';

// First turn you text into a List :
List list = text.split('');

// Shuffle the list :
list.shuffle();

// Then turn back the list into a String
String shuffled = list.join();

print(shuffled); // LLHOE

一行:

String shuffled = ('HELLO'.split('')..shuffle()).join();

扩展方法:

void main() {
  var text = 'HELLO';
  var shuffled = text.shuffled();
  print(shuffled); // OLEHL
}


extension on String {
  String shuffled() =>
    (this.split('')..shuffle()).join();
}