2014-02-09 2 views
6

Kra!
내가, 내 다트 스크립트 중 하나의 출력을 "아름답게"과 같이하고 싶은 :같은 문자를 루프없이 여러 번 인쇄하십시오.

----------------------------------------- 
OpenPGP signing notes from key `CD42FF00` 
----------------------------------------- 

<Paragraph> 

그리고 내가 거기에 특히 간단한 및/또는 가 인쇄 방법 의 최적화 궁금해 Dart의 x 번 같은 문자. 파이썬에서 print "-" * x- 문자 x 번을 인쇄합니다.

이 질문의 목적을 위해, this answer에서 학습, 나는 핵심 Iterable 클래스를 사용하게 다음과 같은 최소한의 코드를 썼다 :

main() { 
    // Obtained with '-'.codeUnitAt(0) 
    const int FILLER_CHAR = 45; 

    String headerTxt; 
    Iterable headerBox; 

    headerTxt = 'OpenPGP signing notes from key `CD42FF00`'; 
    headerBox = new Iterable.generate(headerTxt.length, (e) => FILLER_CHAR); 

    print(new String.fromCharCodes(headerBox)); 
    print(headerTxt); 
    print(new String.fromCharCodes(headerBox)); 
    // ... 
} 

이것은 예상되는 출력을 제공,하지만 이 더 나은입니다 방법 다트에서 문자 (또는 문자열) x 번 인쇄하려면? 필자의 예에서는 - 문자 headerTxt.length 번을 인쇄하려고합니다.

감사합니다.

답변

6

이 방법을 사용합니다.

void main() { 
    print(new List.filled(40, "-").join()); 
} 

귀하의 사례입니다.

main() { 
    const String FILLER = "-"; 

    String headerTxt; 
    String headerBox; 

    headerTxt = 'OpenPGP signing notes from key `CD42FF00`'; 
    headerBox = new List.filled(headerTxt.length, FILLER).join(); 

    print(headerBox); 
    print(headerTxt); 
    print(headerBox); 
    // ... 
} 

출력 :

----------------------------------------- 
OpenPGP signing notes from key `CD42FF00` 
----------------------------------------- 
+0

와우, 확실히 더 읽기 우아한! 나는 당신이했던 것처럼 평범한'List's를 사용하는 더 최적화 된 방법이 있어야만한다고 믿지 않습니다. – Diti

관련 문제