Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Saturday, 20 June 2015

STRINGS (FORMAT) IN C#


Today in Engineer's World, we are discussing a very important topic of C# -String in very easy way. Posted By- +Manish Kumar Gautam +LIV WIRE  +ASP.NET SOLUTIONS


Click imaginationhunt.blogspot to see latest Blogs
 
 
2) Using String.Format() Method:

Format() method reside in String class. It replaces one or more format items in a specified string with the string representation of that object. Return type of Format() method is string.



Let's understand this by seeing an example-

Example- Suppose there is a rectangular shape tar coal container whose length = 40m, and breadth = 15m and height = 10m. You have to find out the capacity it can hold.

Practical implementation:
 
using System;

namespace AspnetSolutions

{
    class FormatString
    {
        public static void
Main() 
         {
            int length = 40;
            int breadth = 15;
            int height = 10;

            int Volume = length * breadth * height; 

             //Line1- Passing output of string.format to input variable

          string input=String.Format("Tar coal container whose length = {0}m, and breadth = {1}m and height = {2}m", length, breadth, height);
            Console.WriteLine(input);

             //Line2- Passing output of string.format to output variable 
            string output = String.Format("Capacity it can hold = {0} cubic meter.", Volume);
            Console.WriteLine(output);
        }
    }


Click imaginationhunt.blogspot to see latest Blogs

Output

In the output window we get the similar output like place holder  in point 1 of string Format() previous blog. But the difference may lie in the coding part. Here we are using the Format() method which may reside in String class. The format method takes the specified string and formats it and return the output in string format. So, to store the output we have used a variable "input" and pass that variable in Console.WriteLine() method, which give us the first string and similarly we format the second string, store it in "output" variable and store it in Console.WriteLine() method, which give us the second string.  
 


Hence, by using Format() method we can also format our string.

Now, look at a real time user input example of format string. 

3) Get user input:

Let us now see a user input program for Format() method.

Practical implementation:

Click imaginationhunt.blogspot to see latest Blogs
  
using System;

namespace AspnetSolutions

{
    class FormatString
    {
        public static void
Main() 
        {
            Console.WriteLine("Enter the Tar coal container measurement");
            Console.Write("Enter the Length = ");
            int len=Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the Breadth = ");
            int width = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the Height = ");
            int high = Convert.ToInt32(Console.ReadLine());

           Console.WriteLine("Tar coal container whose length = {0}m, and breadth = {1}m and height = {2}m", len, width, high);
         Console.WriteLine("Capacity it can hold = {0} cubic meter.", (len*width*high));
        }
    }
}

Output

ReadLine() is used to accept user input string.
Convert.ToInt32 is used to convert string to integer value. 


We'll discuss about Convert.ToInt32 in great details.

To read STRINGS (SUBSTRING) IN C# - Click here.

Related Questions:

#newtoprgm try firstprogram

Q-1 What will be the output of following code:

using System;

namespace AspnetSolutions

{
    class FormatString
    {
        public static void
Main()
        {
           int salary = 40000;
          
string name="Ajay";

           string output = String.Format("Employee: Name = {0} and Salary = {1}", name, salary);
           Console.WriteLine(output);
        }
    }
}

A) Employee Name = Ajay and Salary = System.Int32
B) Explicitly using int and string data type 
C) Employee Name = Ajay and Salary = 40000
D) Employee Name = System.String and Salary = 40000
Ans- Option(C).

Q-2 If we use verbatim literals and escape sequence in Format() method does it create any problem? 

int salary = 40000;string name="Ajay";
string output = String.Format(@"Employee: \nName = {0} and \nSalary = {1}", name, salary);
Console.WriteLine(output);

Ans- It may not create any problem. The program run as expected. By giving its output as- 
Employee: \nName = Ajay and \nSalary = 40000


Click imaginationhunt.blogspot to see latest Blogs

Keep learning and coding...

No comments:

Post a Comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...