Tutorials Navigation
[C#] String Concatenation with Placeholders
Tutorial Name: [C#] String Concatenation with Placeholders
Category: PC Tutorials
Submitted By: Skittle
Date Added:
Comments: 0
Views: 433
Related Forum: PC Building Forum
Share:
String Concatenation is a method of merging strings and values together to get one string in the end. You could do it like this, which is quite inefficient:
int a = 1;
int b = 2;
int c = 3;
string finishedString;
finishedString = "a = " + a + ", b = " + b + ", c = " + c;
//finishedString = "a = 1, b = 2, c = 3"
This is not the best way to go about merging the variables together as it is quite lengths and will take even longer the more values you have. There is a Function called Format which you can use to make this a whole lot simpler, here is how you would use it to get the same output as the first method:
int a = 1;
int b = 2;
int c = 3;
string finishedString;
finishedString = string.Format("a = {0}, b = {1}, c = {2}", a, b, c);
//finishedString = "a = 1, b = 2, c = 3"
This may look more complicated, but it is actually quite simple. You may be wondering, "What do the {0} things do?" The Braces with the numbers inside them are called placeholders, they do not show up in the final string, they are substituted with a different value. As you can see, we have three placeholders ranging from 0 - 2, these will be substituted with our a, b and c variables, like so:
Placeholder | Variable
{0} | a
{1} | b
{2} | c
The place where we want to have the variable a in our string is accompanied by the {0} placeholder! and the same with the other variables. The format of this Function is quite simple, we have the final string with the placeholders as the first parameter, and then the variables we want to substitute in the order. If we only had one variable called myVariable, we would only need one placeholder, {0}, as we start from zero instead of one. This is the code for that example:
string myVariable = "This is a variable";
string finishedString;
finishedString = string.Format("{0}", myVariable);
//finishedString = "This is a variable";
I hope that I have explained this well, it is very simple to understand yet quite difficult to explain. If you have any questions, feel free to PM me
Ratings
Comments
Related Tutorials
- 01. Emulating Xbox 360 on PC for Running COD4 With Mods(3,709)
- 02. How to: Matrix Numbers | Batch File(1,964)
- 03. How to Password Protect Files on Windows(874)
- 04. How to play Socom 2/3/ and Combined Assault on PC(6,937)
- 05. Modern Warfare 2 Vac Ban Bypass Tutorial(6,283)
- 06. How to embed an image on TheTechGame(3,151)
- 07. [PC] NIOH 2 OTHER USER SAVE RESIGN(13,104)
- 08. Host bot lobbies! Full Tutorial!(11,680)
- 09. Unban yourself [Plutonium BO2](14,328)
- 10. Fall Guys - How to Change Your Name Color on Fall Guys(8,422)
- 11. Best Crosshair Settings for Valorant(6,565)
- 12. Othercide The Surgeon Boss Guide(2,577)
- 13. Othercide Remembrances Unlock Guide(4,529)
- 14. Othercide Beginners Tips and Tricks(2,746)
- 15. How to Fix Grounded Crashes, Loading Time, Low FPS and Other(4,899)
"[C#] String Concatenation with Placeholders" :: Login/Create an Account :: 0 comments