Sai A Sai A
Updated date Jul 31, 2023
In this blog, we will explore various methods to accurately convert strings to long in C# with this comprehensive programming blog. Learn how to use built-in methods like long.Parse() and long.TryParse() for error handling, and discover the flexibility of Convert.ToInt64() for general numeric conversions.

Introduction:

In this blog, we will explore various methods to achieve the Strings to Long conversion in C#. By the end of this blog, you will have a complete understanding of different techniques to accurately convert strings to long in C#.

Method 1: Using long.Parse() Method

The simplest and most straightforward method to convert a string to a long in C# is by using the built-in long.Parse() method. This method takes a string as input and returns the corresponding long value. Here's an example of how to use it:

string numberString = "123456789";
long result = long.Parse(numberString);
Console.WriteLine(result);

Output: 

123456789

The long.Parse() method attempts to parse the given string and convert it into a long data type. However, it's crucial to be cautious when using this method, as it can throw an exception if the input string is not a valid representation of a long. To handle such cases gracefully, you can use the long.TryParse() method, as shown in the next method.

Method 2: Using long.TryParse() Method

To ensure a more robust conversion without the risk of exceptions, the long.TryParse() method comes in handy. It attempts to parse the string, and if successful, it assigns the result to the output variable, returning a Boolean value to indicate whether the parsing was successful or not.

string numberString = "987654321";
if (long.TryParse(numberString, out long result))
{
    Console.WriteLine(result);
}
else
{
    Console.WriteLine("Invalid input string.");
}

Output: 

987654321

The long.TryParse() method returns true if the conversion is successful and false if it fails. In case of a successful conversion, the resulting long value is stored in the result variable, which can be used for further operations.

Method 3: Using Convert.ToInt64() Method

Another way to convert a string to a long in C# is by using the Convert.ToInt64() method. This method can handle various numeric representations, making it a versatile option.

string numberString = "5555555555";
long result = Convert.ToInt64(numberString);
Console.WriteLine(result);

Output: 

5555555555

The Convert.ToInt64() method internally calls long.Parse(), so similar precautions should be taken when using it to avoid exceptions.

Method 4: Using long.TryParse() with NumberStyles Enumeration

Sometimes, you might encounter numeric strings with special formatting, such as thousands separators or currency symbols. In such cases, you can use the long.TryParse() method with the NumberStyles enumeration to handle these variations.

string numberString = "1,234,567";
NumberStyles style = NumberStyles.AllowThousands;
CultureInfo culture = CultureInfo.InvariantCulture;
if (long.TryParse(numberString, style, culture, out long result))
{
    Console.WriteLine(result);
}
else
{
    Console.WriteLine("Invalid input string.");
}

Output: 

1234567

By using the NumberStyles enumeration with the long.TryParse() method, we allow parsing of numeric strings with thousands separators (such as commas) without causing an exception.

Method 5: Using Custom Conversion Functions

In certain scenarios, you might encounter non-standard numeric representations in strings. In such cases, you can create custom conversion functions to handle these specific cases.

private static long CustomStringToLong(string numberString)
{
    // Implement custom parsing logic here
    // For example, remove any non-numeric characters and parse the result.
    return long.Parse(numberString);
}

string numberString = "$100,000";
long result = CustomStringToLong(numberString);
Console.WriteLine(result);

Output: 

100000

In the CustomStringToLong() function, you can define your specific logic to extract the numeric value from the string and convert it to a long.

Conclusion:

This blog explored multiple methods to convert strings to long in C#. We started with the straightforward long.Parse() method and learned how to enhance the conversion process by using long.TryParse(), which ensures error-handling without exceptions. We then discovered the flexibility of Convert.ToInt64() for general numeric conversions and explored custom conversion functions for handling unique scenarios.

Comments (0)

There are no comments. Be the first to comment!!!