Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Using array format with strings

Hey Everyone,

I'm still a relatively new C# programmer but I'd love to help other programmers who may not be familiar with something like this. This is a great way to organize stings in a format where it's easily accessible instead of having to scroll through a bunch of things, it also makes things like text to command easier. Without wasting time here's the format to get started:

Code
string soandso = String.Format("",blah,blah);

To analyze this, we are first declaring the string soandso, after that we are setting a blank value for the string although that is not the purpose. Lastly we are using the array system to link something that's currently non existent 👹. Remember the array system starts with 0.

Let's take up a normal example of this:

Code
string soandso = String.Format("Hi {0} How's it going? It's a {1} day out today huh?", "joe", "nice");
Remember since these aren't strings we will still use quotation marks to work with the values.

Now let's use this for user input from a textbox.

Code
string soandso = String.Format("Hey {0} I'm {1}, I am {2} years old and currently live in {3}.", textbox1.text, textbox2.text, textbox3.text, textbox4.text);

That's all. Happy programming everyone.
You can actually do a whole lot more with string.format.

Code
decimal[] prices = new decimal[] {100m, 124.99m, 1.24m };

for(int i = 0; i < prices.Length; i++)
{
    Console.WriteLine("Index {0} {1, 12:C2}", i, prices[i]);
}

Will output:
Quote:Index 0 $100.00
Index 1 $124.99
Index 2 $1.24

It uses this formatting:
Quote:{index[,alignment][ :formatString] }

So for the "{1, 12:C2}" example:
The 1 is just getting the value at index 1 (like normal)
Then the ",12" is adding a 12 character spacing
Finally the ":C2" is formatting the number as currency to two decimal points.

You can also use the :formatString to pass in .ToString arguments.

Code
Guid id = Guid.Empty;
Console.WriteLine("{0}", id);

Will output the standard id.ToString()

Quote:000000-0000-0000-0000-000000

But we can pass in some arguments

Code
Guid id = Guid.Empty;
Console.WriteLine("{0:n}", id);
Console.WriteLine("{0:x}", id);

outputs

Quote:000000
{0x000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

just like this would:
Code
Console.WriteLine(id.ToString("n"));
Console.WriteLine(id.ToString("x"));

More: MSDN
Lotta blood to let, peace to make, give a fuk about a check
BahNahNah beat me to it, there's a lot more you can do with Format. Buuut with C# 6.0 we get some cool new interpolation sugar:

Code
string name = "Odus";
int age = 9;

Console.WriteLine("Hey my name is \{name} and I'm \{age} years old!");
Excellent examples guys. I'll be sure to add them all in the OP when I'm home :).
You can also use string interpolation.
This
Code
string soandso = String.Format("Hi {0} How's it going? It's a {1} day out today huh?", "joe", "nice");
Could look like
Code
string soandso = $"Hi {"joe"} How's it going? It's a {"nice"} day out today huh?";


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by pro_fesnoobSep 24, 2022 04:24 PM 14 796 Sep 24, 2022 04:24 PM
Last Post: BinaryFox
[C] by gurlhackerclubMay 19, 2020 05:18 PM 8 250 May 19, 2020 05:18 PM
Last Post: k1r3n
[Shell] by TimanxdDec 13, 2018 02:32 AM 7 288 Dec 13, 2018 02:32 AM
Last Post: Bobbyx28