0

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?

4
  • 3
    why do you need to define this as an array? Commented Oct 17, 2015 at 15:45
  • 2
    Note that formatting this in your code won't format the output. Commented Oct 17, 2015 at 15:47
  • 1
    It's not clear what you're trying to accomplish. Commented Oct 17, 2015 at 15:47
  • I don't really need to have it as an array, but instead of having each line as a separate string I would like it all in a single array. I understand I wont effect the formatting but it would make it easier a bit for me to do it, I think? Commented Oct 17, 2015 at 15:49

6 Answers 6

6

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

*********************************************";
Sign up to request clarification or add additional context in comments.

Comments

5

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);

3 Comments

I was just going to recommend the same
I didn't see you answer =/
Thanks just what I was looking for, I didn't really need It declaring as an array, I'm just trying to understand the keywords we're using and the different ways they are used.
0

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.

Comments

0

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.

Comments

0

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

1 Comment

Thanks, you were correct about me wanting it centered, also thanks for the link to .NetFiddle that could come in handy
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.