使单词随循环而消失-Dart Programming Languange

时间:2019-03-18 12:36:34

标签: loops dart

示例我编写了这样的代码:

main() {
  var input = 'i want to eat';
  print(input);
  var katas = input.split(' ');
  bool hurufHabis = false;
  var indexHuruf = 0;
  do {
    hurufHabis =false;
    var baris = 'Baris  ${(indexHuruf+1).toString().padLeft(2)} : ';
    for (var kata in katas) {
      if(indexHuruf < kata.length){
        baris += kata[indexHuruf] + '   ';
        hurufHabis = true;
      }else{
        baris +=   '    ';
      }
    }
    indexHuruf++;
    if(hurufHabis)  print(baris);
  } while (hurufHabis);
}

,输出将如下所示:

i want to eat  
Baris   1 : i  w   t   e     
Baris   2 :    a   o   a     
Baris   3 :    n       t       
Baris   4 :    t             

我如何将其更改为这样:

i want to eat
Baris   1 :     w          
Baris   2 :     a      e    
Baris   3 :     n   t  a    
Baris   4 : i   t   o  t     

我无法想象该方法就像任何帮助一样。谢谢

1 个答案:

答案 0 :(得分:1)

一个简单的解决方案是将单词的padLeft设置为全部相同的长度:

var katas = input.split(' ');
var maxLength = katas.map((e) => e.length).reduce(max);
katas = katas.map((e) => e.padLeft(maxLength)).toList();