2015-10-17 82 views
0

我是編程新手,最近我開始了計算機科學學位,我正在努力學習一些代碼,目前我正在學習c#。C#字符串數組

我想獲得一個數組中的字符串,以減少代碼的數量,並使其更容易在控制檯中進行格式化。

我的代碼是:

string [] sInvite = new string[] 
     { 
      "*********************************************" 

           +sGuest+ 
        "is invited to the wedding of:" 
        + sBride + " and " + sGroom + 
       "On Saturday 17 July 2016 at 2:00pm", 

      "*********************************************" 
     }; 

這是我如何將它輸出

Console.WriteLine(sInvite); 

,這是在控制檯中的實際產量,顯然不是我想要的東西

system.String[] 

關於如何讓這個工作或者我做錯了什麼想法?

+3

爲什麼你需要將其定義爲一個數組? –

+2

請注意,在代碼中對其進行格式化將不會格式化輸出。 – FirstOne

+1

目前尚不清楚你想要完成什麼。 – ryanyuyu

回答

5

使用逐字字符串是這樣的:

string sInvite = @" 

********************************************* 

        " + sGuest + @" 
     is invited to the wedding of: 
     " + sBride + @" and " + sGroom + @" 
    On Saturday 17 July 2016 at 2:00pm 

*********************************************"; 

Console.WriteLine(sInvite); 
+0

我只是要推薦相同的 –

+0

我沒有看到你答案=/ – LINQ

+0

感謝我正在尋找的東西,我真的不需要它聲明爲一個數組,我只是想了解關鍵字我們'正在使用和他們使用的不同方式。 – Chris

0

您創建了一個包含單個元素的字符串數組。你也可以將該字符串存儲爲字符串並打印出來。如果你想繼續使用一個數組,你可以做的一件事是string.Join()數組元素以及像\ n這樣的分隔符。然後打印結果字符串。

0

你不需要在這種情況下,一個字符串數組。你可以使用原義字符串,這樣

string sInvite = 
    @"*********************************************" 

          +sGuest+ 
       "is invited to the wedding of:" 
       + sBride + " and " + sGroom + 
      "On Saturday 17 July 2016 at 2:00pm", 

     "*********************************************"; 

@可以讓你在兩個或兩個以上行寫string

如果你堅持使用數組,你可以做Console.WriteLine(String.Join(" ", sInvite));,它會將你的數組轉換成string,使用第一個數組作爲數組位置的分隔符。

6

如果您使用的是C#6,然後你可以從雅各布·馬薩德採取answer,而使用string interpolation

string sInvite = [email protected]" 
********************************************* 

        {sGuest} 
     is invited to the wedding of: 
     {sBride} and {sGroom} 
    On Saturday 17 July 2016 at 2:00pm 

*********************************************"; 
0

我去不同的路線上比這裏所有的其他的答案,以確保您的文字總是居中。我猜你是想從你格式化你的代碼的方式來這樣做。因此,這裏是我的答案:

int width = 45; 
string sGuest = "Nasreddine"; 
string sBride = "Jane"; 
string sGroom = "John"; 

Console.WriteLine(new String('*', width)); 
Console.WriteLine(Center(sGuest, width)); 
Console.WriteLine(Center("is invited to the wedding of:",width)); 
Console.WriteLine(Center(sBride + " and " + sGroom, width)); 
Console.WriteLine(Center("On Saturday 17 July 2016 at 2:00pm", width)); 
Console.WriteLine(new String('*', width)); 

這是確保該文本爲中心的功能:

public static string Center(string str, int length) 
{ 
    if (string.IsNullOrWhiteSpace(str)) 
    { 
     return new String(' ',length); 
    } 

    if (str.Length >= length) 
    { 
     return str; 
    } 

    var halfDiff = (length - str.Length)/2.0; 
    return string.Format("{0}{1}", new String(' ', (int) Math.Floor(halfDiff)), str) ; 
} 

這裏首先是一個live demo

+0

謝謝,你對我的要求是正確的,也感謝鏈接到.NetFiddle,可以派上用場 – Chris

0

第一件事情,你並不需要一個數組來減少代碼。其次,在這之前給出的答案是做你想達到的一種新方式,即逐字串。這裏是做的老式方法:

var string1 = String.Format(@" 
********************************************* 
        {0} 
     is invited to the wedding of: 
      {1} and {2} 
    On Saturday 17 July 2016 at 2:00pm 

*********************************************",sGuest,sBride,sGroom); 

Console.Writeline(string1);