Reputation: 27
I'm new to programming, I recently started a computer science degree and I'm struggling a bit with the code, I am currently learning c#.
I am trying to get a string in an array to cut down on the amount of code and make it easier to format it in the console.
my code is:
string [] sInvite = new string[]
{
"*********************************************"
+sGuest+
"is invited to the wedding of:"
+ sBride + " and " + sGroom +
"On Saturday 17 July 2016 at 2:00pm",
"*********************************************"
};
This is how I output it
Console.WriteLine(sInvite);
and this is the actual output in the console, obviously not what I wanted
system.String[]
any ideas on how I can get this to work, or what I am doing wrong?
Upvotes: 0
Views: 251
Reputation: 972
First thing first, you do not need an array to reduce the code. Secondly the answers given prior to this one are a new way of doing what you want to achieve i.e verbatim string. Here is the old fashioned way of doing it:
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);
Upvotes: 0
Reputation: 37908
I went on different route than all the other answers here to make sure that your text was always centered. I guessed that you wanted that from the way you formatted your code. So here's my answer:
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));
And this is the function that makes sure that the text is centered:
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) ;
}
And here's a live demo
Upvotes: 0
Reputation: 119206
If you are using C# 6 then you can take the answer from Yacoub Massad and instead use string interpolation:
string sInvite = $@"
*********************************************
{sGuest}
is invited to the wedding of:
{sBride} and {sGroom}
On Saturday 17 July 2016 at 2:00pm
*********************************************";
Upvotes: 6
Reputation: 434
You don't need a string array in this case. You could use a verbatim string, like this
string sInvite =
@"*********************************************"
+sGuest+
"is invited to the wedding of:"
+ sBride + " and " + sGroom +
"On Saturday 17 July 2016 at 2:00pm",
"*********************************************";
The @ allows you to write a string
in two or more lines.
If you insist in use array, you can do Console.WriteLine(String.Join(" ", sInvite));
, it will convert your array in a string
using the first as a separator for the array positions.
Upvotes: 0
Reputation: 27871
Use a verbatim string like this:
string sInvite = @"
*********************************************
" + sGuest + @"
is invited to the wedding of:
" + sBride + @" and " + sGroom + @"
On Saturday 17 July 2016 at 2:00pm
*********************************************";
Console.WriteLine(sInvite);
Upvotes: 5
Reputation: 4347
You created a string array containing a single element. You might as well just store that string as a string and print it. One thing you could do, if you want to keep using an array, is string.Join() the array elements together with a separator like \n. Then print that resulting string.
Upvotes: 0