41
Getting started with C# -String Formatting
Hey friends, today I will show you how string formatting works in C#; let's dive in.
You can follow long with your own code or clone the git repo as well. gettingStartedWIthCsharp - stringFormatting
All right, so let's learn a little .NET CLI as well; we start by making a new .NET project. Now let's create our .NET project.
//The following will create our console app in the directory, in the stringFormate.
dotnet new console -o stringFormate
This basic console app for now, and we will be adding upon this. Here our Main
method will be asking for your first and last name, and then it will give you your initials.
using System;
namespace string format
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter you first name: ").IsUpper;
string firstName = Console.ReadLine();
Console.WriteLine("Enter you last name: ");
string lastName = Console.ReadLine();
Console.WriteLine($"Your initials are: '{firstName[0]}.{lastName[0]}'");
}
}
}
If we look at the existing code block that we just ran, we can see that the final line seems a little different from a standard print string. This is called string interpolation
; this is a way of formatting our output; we can add the $""
to our write lines to call variables from directly in the Console.WriteLine()
method.
Console.WriteLine($"Your initials are: '{firstName[0]}.{lastName[0]}'");
You can access the characters in a string by referring to its index number inside square brackets []
. If we take a look at our example, we can see that we already used this to get the first character from our first and the last name gets it our initials.
Console.WriteLine($"Your initials are: '{firstName[0]}.{lastName[0]}'");
Okay, now let's look at a few other methods that can help us format our stings.
This is a method that you can add to your strings to make sure that you always get upper case lettering no matter what the user puts in. If we take a tool at the following code block, we can see that we need to add some extra variables to our method. If we look at our new variables, we can see that we call the ToUpper()
method when it's attached to an existing variable. ie fistName.ToUpper()
Side Note
C# is a strongly typed language which means, you must declare a variable type before you can use it. We see that with both the variables, we are calling the types we want to use. The "var" keyword is used to declare a var type variable. The var type variable can be used to store a simple .NET data type, a complex type, an anonymous type, or a user-defined type.
From a programming standpoint, you can think of var == string, double, int, char, bool, long, object
.
Console.WriteLine("Enter you first name: ");
string firstName = Console.ReadLine();
var upperFirstName = firstName.ToUpper();
Console.WriteLine("Enter you lastname: ");
string lastName = Console.ReadLine();
var upperLastName = lastName.ToUpper();
Console.WriteLine($"Your initials are: '{upperFirstName[0]}.{upperLastName[0]}'");
We can see that in this session, we were able to complete the following.
- Being able to create variables.
- Taking input from the console.
- Apply indexing to our outputs to get only certain characters.
- Using different types.
- Formatting methods.
- Using string interpolation to format our output.
Formatting strings can be instrumental in real-world
scenarios; this could range from formatting numbers for security reasons or validating that the data that is being used is only received in a specific format itself.
Here is some homework for you. Are you able to find any other method that we can use to format our data? I will give you one more to look into to, .ToLower
the name gives it away if you have made it this far; with this, we can turn all our data into a lower casing format. Explore and see what other methods you can find as well.
Like, share and follow me 🔥 for more content:
Get out there and build and happy coding.
41