jay's old blog

this blog will be deleted soon - please visit my new blog - https://thesanguinetechtrainer.com

String Formatting

Ah! The most useful part of displaying stuff. The thing is string formatting is a topic on its own. Like, you could keep getting better at it and there would still be something to write about.  

{position} 

One of the first things I use when formatting a string is using the {position} thing. When you want to display a string which has to intersped with variable values, you may choose to concatenate them. This could get cumbersome when you wish to include too many variables. That is when you can use {} and you put a location value at that spot.  

The number inside the brackets indicates the location of the parameters on the right side of the console writeline. 

Culture Info 

The 'culture info' is an option that allows you to let the string method automatically display information in a culturally relevant way. For instance, when the culture is set to 'en-US' and a currency is being displayed, the string will be automatically formatted as dollars. This also applies to stuff like date and time which are all culturally sensitive 

formatting key 

Now, there is no such thing as a formatting key. I am just making this word because depending on the key, the display will change just like it does when a culture info component is attached. 

custom formatting key 

By default, some components have a default key to use. Like date objects have 'd', 'D', 'M' which accordingly change the display.  

Just like those formatting keys, you can build your own formatting keys that can be used to decide how the display works. Of course, you will use them with the tostring() definition.  

As always, you can check our code here that shows all these in action.


Follow me on twitter, facebook and instagram for more updates. Thanks!

Comments are closed