Extension Methods – Helpful parse methods for string

Yan Cui

I help clients go faster for less using serverless technologies.

   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: }


 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


Leave a Comment

Your email address will not be published. Required fields are marked *