Extension Methods – Helpful parse methods for string

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

I never fully recovered my workspace setup when I upgraded my laptop two years ago, and I still miss things today. If only I had known about Gitpod back then…

Learn more

   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 3 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  3. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

Leave a Comment

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