13.5. Parsing
Date parsing is an attempt to parse a string formatted as a date. Numbers can also be parsed. Listing 13.10 shows parsing of dates and numbers. Typically in Java, a good way to quickly check whether a string is a date or a number is to parse it and check for any exceptions thrown. The same technique works in C#.
Listing 13.10. Parsing of Numbers and Dates (C#)
using System; public class Test { public static void Main(string[] args) { Console.WriteLine(Int32.Parse("556")); try { Console.WriteLine(Double.Parse("45764")); } catch (FormatException e) { Console.WriteLine(e.StackTrace); } try { Console.WriteLine(DateTime.Parse("12/3/2002")); } catch (FormatException e) { Console.WriteLine(e.StackTrace); } } } |
Listing 13.10 shows a ...
Get .NET for Java Developers: Migrating to C# now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.