30秒学会 Dart 片段 · 2018年5月2日

30秒学会 Dart 片段 – mapString

Creates a string with the results of calling the provided function on every character in the given string.

Use String.split('') and Iterable.map() to call the provided function, fn, for each character in str.
Use Iterable.join('') to recombine the list of runes into a string.

代码实现

String mapString(String str, String Function(String c) fn) {
  return str.split('').map(fn).join('');
}

使用样例

mapString('lorem ipsum', (c) => c.toUpperCase()); // 'LOREM IPSUM'