Extension Methods – Helpful parse methods for string

   1: /// <summary>

   2: /// Parses a string as a short

   3: /// </summary>

   4: public static short ParseShort(this string str, bool throwOnOverflow = true)

   5: {

   6:     return str.ParseAsStruct(Convert.ToInt16, throwOnOverflow);

   7: }

   8:  

   9: /// <summary>

  10: /// Parses a string as an ushort

  11: /// </summary>

  12: public static ushort ParseUshort(this string str, bool throwOnOverflow = true)

  13: {

  14:     return str.ParseAsStruct(Convert.ToUInt16, throwOnOverflow);

  15: }

  16:  

  17: /// <summary>

  18: /// Parses a string as an int

  19: /// </summary>

  20: public static int ParseInt(this string str, bool throwOnOverflow = true)

  21: {

  22:     return str.ParseAsStruct(Convert.ToInt32, throwOnOverflow);

  23: }

  24:  

  25: /// <summary>

  26: /// Parses a string as an uint

  27: /// </summary>

  28: public static uint ParseUint(this string str, bool throwOnOverflow = true)

  29: {

  30:     return str.ParseAsStruct(Convert.ToUInt32, throwOnOverflow);

  31: }

  32:  

  33: /// <summary>

  34: /// Parses a string as a long

  35: /// </summary>

  36: public static long ParseLong(this string str, bool throwOnOverflow = true)

  37: {

  38:     return str.ParseAsStruct(Convert.ToInt64, throwOnOverflow);

  39: }

  40:  

  41: /// <summary>

  42: /// Parses a string as an ulong

  43: /// </summary>

  44: public static ulong ParseUlong(this string str, bool throwOnOverflow = true)

  45: {

  46:     return str.ParseAsStruct(Convert.ToUInt64, throwOnOverflow);

  47: }

  48:  

  49: /// <summary>

  50: /// Parses a string as a float

  51: /// </summary>

  52: public static float ParseFloat(this string str, bool throwOnOverflow = true)

  53: {

  54:     return str.ParseAsStruct(Convert.ToSingle, throwOnOverflow);

  55: }

  56:  

  57: /// <summary>

  58: /// Parses a string as a double

  59: /// </summary>

  60: public static double ParseDouble(this string str, bool throwOnOverflow = true)

  61: {

  62:     return str.ParseAsStruct(Convert.ToDouble, throwOnOverflow);

  63: }

  64:  

  65: /// <summary>

  66: /// Parses a string as a date time value

  67: /// </summary>

  68: public static DateTime ParseDateTime(this string str)

  69: {

  70:     return str.ParseAsStruct(DateTime.Parse);

  71: }

  72:  

  73: /// <summary>

  74: /// Parses a string as a date time value using the exact format string

  75: /// </summary>

  76: public static DateTime ParseDateTimeExact(this string str, string format)

  77: {            

  78:     return str.ParseAsStruct(s => DateTime.ParseExact(s, format, CultureInfo.InvariantCulture));

  79: }

  80:  

  81: /// <summary>

  82: /// Parses a string as a boolean value

  83: /// </summary>

  84: public static bool ParseBoolean(this string str)

  85: {

  86:     return str.ParseAsStruct(Convert.ToBoolean);

  87: }

  88:  

  89: /// <summary>

  90: /// Parses a string as an enum value

  91: /// </summary>

  92: public static TEnum ParseEnum<TEnum>(this string str, bool ignoreCase = true) where TEnum : struct

  93: {

  94:     TEnum result;

  95:     return Enum.TryParse(str, ignoreCase, out result) ? result : default(TEnum);

  96: }        

  97:  

  98: private static T ParseAsStruct<T>(

  99:     this string str, Func<string, T> convert, bool throwOnOverflow = true) 

 100:     where T : struct

 101: {

 102:     if (throwOnOverflow)

 103:     {

 104:         checked

 105:         {

 106:             return convert(str);

 107:         }

 108:     }

 109:  

 110:     unchecked

 111:     {

 112:         return convert(str);

 113:     }

 114: }

Enjoy what you’re reading? Subscribe to my newsletter and get more content on AWS and serverless technologies delivered straight to your inbox.


Yan Cui

I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.

You can contact me via Email, Twitter and LinkedIn.

Hire me.


Check out my new course, Complete Guide to AWS Step Functions.

In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, design patterns and best practices.

Get Your Copy