2016-05-06 13 views
2

私はC#の新機能で何を検索するのか理解できませんでした。私は文字列のリストを別の文字列リストの右側にある "列"に印刷しようとしています。右側の「列」にある2つの文字列には、それぞれ独自の行が必要な複数の値があります。しかし、私がこれをしようとすると、自分のラインを必要とする値は、その "コラム"内にとどまるのではなく、左上に終わります。ここで列の外に書式化された文字列 "spilling"

は、私が見てみたいものです。

そして、ここでは、私が何を得るのです。

static void Main(string[] args) 
{ 
    string ut = "1Y, 4D, 01:23:45"; 
    string met = "T+4D, 01:11:32"; 
    string vesselName = "Saturn K"; 
    string vesselModules = "Command/Service Module \nMunar Excursion Module"; 
    string kerbals = "Valentina Kerman, Pilot; \nJebediah Kerman, Pilot; \nBill Kerman, Engineer; \nBob Kerman, Scientist"; 

    string[] headerNames = { "UT:", "MET:", "Vessel:", "Modules:", "Crew:" }; 
    string[] headerData = new string[5] { ut, met, vesselName, vesselModules, kerbals }; 

    for (int index = 0; index < headerNames.Length; index++) 
     Console.WriteLine("{0,-12} {1,-46}", headerNames[index], headerData[index]); 
} 

答えて

0

問題は、1つの列に複数の項目があることです。新しい行を作成するのではなく、項目を分割することで簡単に解決できます。 \nの代わりに;を使用しています。コメントをもとに

string ut = "1Y, 4D, 01:23:45"; 
    string met = "T+4D, 01:11:32"; 
    string vesselName = "Saturn K"; 
    string vesselModules = "Command/Service Module;Munar Excursion Module"; 
    string kerbals = "Valentina Kerman, Pilot;Jebediah Kerman, Pilot;Bill Kerman, Engineer;Bob Kerman, Scientist"; 

    string[] headerNames = { "UT:", "MET:", "Vessel:", "Modules:", "Crew:" }; 
    string[] headerData = new string[5] { ut, met, vesselName, vesselModules, kerbals }; 
    for (int index = 0; index < headerNames.Length; index++) 
    { 
     // Split data if there are more than one. 
     var items = headerData[index].Split(';'); 
     if (items.Length > 1) 
     { 
      // We got more than one item. Render first item. 
      Console.WriteLine("{0,-12} {1,-46}", headerNames[index], items[0]); 
      for (int i = 1; i < items.Length; i ++) 
      { 
       // Render the following items. 
       Console.WriteLine("{0,-12} {1,-46}", string.Empty, items[i]); 
      } 

     } 
     else 
     { 
      // Only one item. Render it as usual. 
      Console.WriteLine("{0,-12} {1,-46}", headerNames[index], headerData[index]); 
     } 
    } 

enter image description here

編集:

Console.WriteLinestring.Format()パターンを使用しています。 Consoleから:基本的には、渡されたパラメータを持つすべての{}を置き換えると言う

// Writes out a formatted string and a new line. Uses the same 
// semantics as String.Format. 
// 
public virtual void WriteLine (String format, params Object[] arg) 
{ 
    WriteLine(String.Format(FormatProvider, format, arg)); 
} 

。プレースホルダーとして{}を表示できます。私は単純に空の値を使うべきだと言った。

例:

Console.WriteLine("{0} - {1} - {2}", "One", "Two", "Three"); 

出力:

One - Two - Three 

これは非常に問題になっている例のようなものですが、問題も{0,-12}を使用して、表示されるべき文字の数を定義します。私がしたことは、単に ""のような空の文字列を渡すことです。

Console.WriteLine("{0} - {1} - {2}", "One", string.Empty, "Three"); 

出力:

One - - Three 

そして、私のコードで私は単に最初の12の文字は何に置き換えされるべきではないと言うが、それでも12個の文字を使用します。

Console.WriteLine("{0,-12} {1,-46}", string.Empty, "Some value"); 

あなたはここString.Formatの詳細を読むことができます:

http://www.dotnetperls.com/format

インクリメントに関して、私がしたのはあなたの値で配列を埋めることでした。それが2つ以上の値であることが検出された場合、私は2行目(およびそれに続くすべて)に「空のヘッダ」がなければならないことを知っています。 string.EmptyをConsole.WriteLineに渡すと、空のヘッダー(ただしヘッダー)がレンダリングされます。

擬似コード:

Split by ';'. 
Is there more than one item? If yes, goto 1. If no, goto 2. 

1. Render first line with header and first data. 
1.1 Render all the other lines, but with an empty header (still taking up 12 chars). 

2. Render line with header and data. 
+0

恐ろしいです!本当にありがとう!簡単な質問:私はstring.Splitを取得すると、その文字をマーカーとして使用して文字列を分割しますが、実際に増分とstring.Emptyパーツは何を理解していません。 "items"の長さに "i"をインクリメントすると、 ";"次の行に?私はMSDNのガイドを読むのに最善を尽くしていますが、私はそれだけを理解しています。 – blorgon

+0

@blorgon私は自分の答えを編集しました。私はそれが論理を説明することを願っています。そうでない場合は、もう一度お尋ねください。 – smoksnes

+0

うわー!上およびそれ以上!本当にありがとう。 – blorgon

0

問題ここに:

そしてここでは、私のコードですあなたはですとkerbalsには\nがあります。コードは\nの0列から始まるので、新しい行に移動します。したがって、\nを削除しようとすると正常に動作します。

+0

これは、すべてのvesselModuleとすべてのkerbalsを同じ行に出力しますが、これは目的の出力ではありません。 – smoksnes

0

これらの文字列を配列で分割し、ネストループを使用してこれらの文字列を改行で印刷することができます。

static void Main(string[] args) 
{ 
    string ut = "1Y, 4D, 01:23:45"; 
    string met = "T+4D, 01:11:32"; 
    string vesselName = "Saturn K"; 
    string vesselModules = "Command/Service Module \nMunar Excursion Module"; 
    string kerbals = "Valentina Kerman, Pilot; \nJebediah Kerman, Pilot; \nBill Kerman, Engineer; \nBob Kerman, Scientist"; 

    string[] headerNames = { "UT:", "MET:", "Vessel:", "Modules:", "Crew:" }; 
    string[] headerData = new string[5] { ut, met, vesselName, vesselModules, kerbals }; 

    for (int index = 0; index < headerNames.Length; index++) { 
     if(headerNames[index] == "Modules:" || headerNames[index] == "Crew:") { 
      string dataStr = headerData[index]; 
      var data = dataStr.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); 
      Console.WriteLine("{0,-12} {1,-46}", headerNames[index], data[0] ?? string.Empty); 
      for (int i = 1; i < data.Length; i++) { 
       Console.WriteLine("{0,-12} {1,-46}", string.Empty, data[i]); 
      } 
     } else { 
      Console.WriteLine("{0,-12} {1,-46}", headerNames[index], headerData[index]); 
     } 
    } 
} 
+0

は\ fを使用してこれを返します。 http://puu.sh/oI3aR/efe6eb879c.png – blorgon

+0

更新された回答を参照してください –

0

私は私の例のようにそれを解決するだろう。上に列の区切りとテーブルの幅を定義することができます

class Program 
{ 
    static int tableWidth = 77; 
    static string columnSeparator = string.Empty; 

    static void Main(string[] args) 
    { 
     string ut = "1Y, 4D, 01:23:45"; 
     string met = "T+4D, 01:11:32"; 
     string vesselName = "Saturn K"; 
     string vesselModules = "Command/Service Module \nMunar Excursion Module"; 
     string kerbals = "Valentina Kerman, Pilot; \nJebediah Kerman, Pilot; \nBill Kerman, Engineer; \nBob Kerman, Scientist"; 

     string[] headerNames = { "UT:", "MET:", "Vessel:", "Modules:", "Crew:" }; 
     string[] headerData = new string[5] { ut, met, vesselName, vesselModules, kerbals }; 

     for (int index = 0; index < headerNames.Length; index++) 
     { 
      var multiCol = headerData[index].Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); 
      int counter = 0; 
      foreach (var col in multiCol) 
      { 
       PrintRow(new string[] { counter == 0 ? headerNames[index] : string.Empty, col }); 
       counter++; 
      } 
     } 

     Console.Read(); 
    } 

    static void PrintRow(params string[] columns) 
    { 
     int width = (tableWidth - columns.Length)/columns.Length; 
     string row = columnSeparator; 

     foreach (string column in columns) 
     { 
      row += AlignLeft(column, width) + columnSeparator; 
     } 

     Console.WriteLine(row); 
    } 

    static string AlignCentre(string text, int width) 
    { 
     text = text.Length > width ? text.Substring(0, width - 3) + "..." : text; 

     if (string.IsNullOrEmpty(text)) 
     { 
      return new string(' ', width); 
     } 
     else 
     { 
      return text.PadRight(width - (width - text.Length)/2).PadLeft(width); 
     } 
    } 

    static string AlignLeft(string text, int width) 
    { 
     text = text.Length > width ? text.Substring(0, width - 3) + "..." : text; 

     if (string.IsNullOrEmpty(text)) 
     { 
      return new string(' ', width); 
     } 
     else 
     { 
      return text.PadRight(width); 
     } 
    } 
} 
関連する問題