2016-03-01 35 views
-2

我看了所有的地方,并没有看到一个回答这个具体问题...我写在C#中的Windows控制台应用程序,并想借此一组字符串并使用换行将它们格式化为固定宽度的列。返回的字符串应该用空格填充,并在每行末尾有一个新的行字符。C#格式化文本与包装

下面是一个例子。我有3个文本字符串,我想通过包装将它们合并到3个固定宽度的列中。

string text10 = "abcdefghij"; 
string text15 = "123456789"; 
string text6 = "zxywvu"; 

我想将其格式化为3列宽度为4 - 10 - 5(与他们之间的空间)。举个例子:

format(text10, test15, text5) 

将返回此:

abcd 1234567890 zxywv\n 
efgh 13245  u \n 
ij     \n 

有谁知道一个简单的方法使用.NET库做到这一点还是我将不得不为此编写一个文本格式化功能东东?

+3

对于什么样的应用? Winforms,Webforms,MVC ...? –

+0

我的不好。更新一提的是我在C#我的工作,愿与英寸 – Corez

回答

0

这正是那种有因为有程序员许多答案的问题。所以我也想尝试!如果您需要更改列的宽度或事件的数量,那么类似这样的事情呢?

public static string PopFrontPadded(ref string oStr, int iPadding) 
    { 

     // Argument check 
     if (iPadding <= 0) 
      throw new ArgumentException("iPadding <= 0"); 

     // Handle idiots 
     if (oStr == null) 
      return "".PadRight(iPadding); 

     // Pop front & pad 
     string oRes = new string(oStr.Take(Math.Min(oStr.Length, iPadding)).ToArray()); 
     oStr = oStr.Substring(oRes.Length); 
     return oRes.PadRight(iPadding); 
    } 

    public static string format(string oSeparator, string oNewLine, string[] oStrings, int[] oWidths) 
    { 

     // Null checks 
     if (oStrings == null || oWidths == null || oSeparator == null || oNewLine == null) 
      throw new ArgumentException("oStrings == null || oWidths == null || oSeparator == null || oNewLine == null"); 

     // Length check: Must be same amount of widths as there are strings 
     if (oStrings.Length != oWidths.Length) 
      throw new ArgumentException("oStrings.Length != oWidths.Length"); 

     // All widths must be > 0 
     foreach (int i in oWidths) 
     { 
      if (i <= 0) 
       throw new ArgumentException("width must be > 0"); 
     } 


     var sb = new System.Text.StringBuilder(); 
     List<string> oList = new List<string>(oStrings); 

     // Loop while oList contains even one string with length > 0 
     do 
     { 
      // For all given strings 
      for (int i = 0; i < oList.Count; i++) 
      { 

       // PopFrontPadded 
       string oTmp = oList[i]; 
       sb.Append(PopFrontPadded(ref oTmp, oWidths[i])); 
       oList[i] = oTmp; 

       // Append separator 
       if (i < oList.Count - 1) 
        sb.Append(oSeparator); 
      } 

      // Append New Line 
      sb.Append(oNewLine); 

     } 
     while (oList.Find(x => x.Length > 0) != null); 

     return sb.ToString(); 
    } 

    [STAThread] 
    static void Main() 
    { 
     string text10 = "abcdefghij"; 
     string text15 = "123456789"; 
     string text6 = "zxywvu"; 

     string oResult = format(
      " ",          // Separator 
      Environment.NewLine,      // Line break 
      new string[3] { text10, text15, text6 }, // strings 
      new int[3] {  4,  10,  5 }); // column widths 

     Console.WriteLine(oResult); 
     /* Outputs 
     * 
     * abcd 1234567890 zxywv 
     * efgh 12345  u  
     * ij         
     * 
     */ 
    } 
0

下面是做到这一点的一种方法:

string format(string col1, string col2, string col3) 
{ 
    var sb = new StringBuilder(); 
    while(col1.Length > 4 || col2.Length > 10 || col3.Length > 5) 
    { 
     sb.AppendLine(CreateLine(ref col1, ref col2, ref col3)); 
    } 
    return sb.ToString(); 
} 

string CreateLine(ref string col1, ref string col2, ref string col3) 
{ 
    var returnValue = string.Format("{0} {1} {2}", 
          col1.Substring(0, Math.Min(col1.Length, 4)).PadRight(4), 
          col2.Substring(0, Math.Min(col2.Length, 10)).PadRight(10), 
          col3.Substring(0, Math.Min(col3.Length, 5)).PadRight(5)); 
    if (col1.Length > 4) 
     col1 = col1.Substring(4, col1.Length - 4); 
    if (col2.Length > 10) 
     col2 = col2.Substring(10, col2.Length - 10); 
    if(col3.Length > 5) 
     col3 = col3.Substring(5, col3.Length - 5); 
    return returnValue; 
} 

注:
1.要显示此正确,你将不得不使用像仪固定等宽字体。
2.如果要使用此上的浏览器,你要么需要使用&nbps而不是空格或与<pre>标签包装的结果。

+0

添加了相应的空格和换行字符,这将除了在COL1为3个字符的情况下工作的字符串,COL2为9个字符,COL3是4个字符...不是? – Corez

+0

我实际上只在qurstion测试的数字,但我认为这与前人的精力其他lebgths正常工作。 –

1

这里是另一种方式来做到这一点使用LINQ:

string format(string column1, string column2, string column3) 
    { 
     int column1Width = 4; 
     int column2Width = 10; 
     int column3Width = 5; 
     int loopCount = 0; 

     StringBuilder output = new StringBuilder(); 

     while (true) 
     { 
      string col1 = new string(column1.Skip<char>(loopCount * column1Width).Take<char>(column1Width).ToArray()).PadRight(column1Width); 
      string col2 = new string(column2.Skip<char>(loopCount * column2Width).Take<char>(column2Width).ToArray()).PadRight(column2Width); 
      string col3 = new string(column3.Skip<char>(loopCount * column3Width).Take<char>(column3Width).ToArray()).PadRight(column3Width); 

      //Break out of loop once all col variables contain only white space 
      if (String.IsNullOrWhiteSpace(col1) && String.IsNullOrWhiteSpace(col2) && String.IsNullOrWhiteSpace(col3)) 
      { 
       break; 
      } 

      output.AppendFormat("{0} {1} {2}\n", col1, col2, col3); 
      loopCount++; 
     } 
     return output.ToString(); 
    }