Saturday, October 22

Parsing the most commonly used technique in c-Sharp and Higher Level Languages


Parsing term actually refers to the coversion from one data type to another. In programming parsing technique is very important while concering with user data of different data types. You need to convert a string containing char digits into an integer, allowing you to use a numeric value. There are several methods, such as int.Parse, that are ideal for this. Here we look at three useful methods that are effective, assuming your code must only accept very simple input strings, using code in the C# programming language.

String input data:
"999" // input is integer
"-26" //input is signed integer
"abc" //string is the input value
"0.123" //input is of float type




After applying suitable integer parsing technique the output is:
999 // input string is successfully converted into integer without any error(underflow & overflow)
-5 // input string is converted into signed integer
Error // as the input was string (combination of characters) while parsing into integer data type it gives an error
Error // input value is float and can not be parsed into integer

Example
First, here we see the int.Parse method. int.Parse is the simplest method, and is also my favorite for many situations. It throws exceptions on invalid input, which can be slow if they are common. It is does not contain any internal null checks.

Example program that uses int.Parse [C#]

using System;


class Program
{
    static void Main()
    {
// Convert string to integer
string text = "999";
int num = int.Parse(text); // static method Parse is used to convert string into integer(unsigned int)
Console.WriteLine(num); // Simple Display method is called
    }
}

Output
999

int.TryParse
One of the most useful methods for parsing integers in the C# language is the int.TryParse method. This method works the same way as int.Parse as shown above, but it uses somewhat more confusing syntax and does not throw exceptions. You must describe the second parameter with the out modifier, and TryParse also returns true or false based on its success. This method is described in more detailed on another page.

int.TryParse Method, Parse Integers Correctly
Convert.ToInt32 example
Third, we look at the Convert.ToInt32 method. Convert.ToInt32, along with its siblings Convert.ToInt16 and Convert.ToInt64, is actually a static wrapper method for the int.Parse method. It can be slower than int.Parse if the surrounding code is equivalent.

Convert.ToInt32
Example program that uses Convert.ToInt32 [C#]


using System;


class Program
{
    static void Main()
    {
// Convert 'text' string to an integer with Convert.ToInt32.
string text = "999";
int num = Convert.ToInt32(text);
Console.WriteLine(num);
    }
}

Output
999
Parsing is very important technique in C-sharp and higher languages this technique is most commonly used. Its syntax is more confusing. The syntax for this method may be more confusing as it uses the bit size of the int, which may not be really important for some of your code. The .NET Framework can better deal with that sort of detail. It is slower and more complicated. These Convert methods don't seem to add any value here, and may be harder to read for some. It is probably better to focus more on the faster and simpler functions.i hope you have learned alot from it. Post your comments. Thank you

No comments:

Post a Comment