Skip to content

Instantly share code, notes, and snippets.

@pierre3
Created August 21, 2013 14:28
Show Gist options
  • Save pierre3/6295195 to your computer and use it in GitHub Desktop.
Save pierre3/6295195 to your computer and use it in GitHub Desktop.
string to primitive

using System;
using System.Linq;
using System.Text;
using System.Globalization;
namespace TextDataUtility
{
public static partial class StringExtensions
{
public static byte ToByteOrDefault(this string s)
{
byte result;
if(byte.TryParse(s,out result))
{
return result;
}
return default(byte);
}
public static byte ToByteOrDefault(this string s, byte defaultValue )
{
byte result;
if(byte.TryParse(s,out result))
{
return result;
}
return defaultValue;
}
public static byte ToByteOrDefault(this string s, NumberStyles numberStyles, IFormatProvider formatProvider, byte defaultValue)
{
byte result;
if(byte.TryParse(s,numberStyles, formatProvider,out result))
{
return result;
}
return defaultValue;
}
public static short ToInt16OrDefault(this string s)
{
short result;
if(short.TryParse(s,out result))
{
return result;
}
return default(short);
}
public static short ToInt16OrDefault(this string s, short defaultValue )
{
short result;
if(short.TryParse(s,out result))
{
return result;
}
return defaultValue;
}
public static short ToInt16OrDefault(this string s, NumberStyles numberStyles, IFormatProvider formatProvider, short defaultValue)
{
short result;
if(short.TryParse(s,numberStyles, formatProvider,out result))
{
return result;
}
return defaultValue;
}
public static ushort ToUInt16OrDefault(this string s)
{
ushort result;
if(ushort.TryParse(s,out result))
{
return result;
}
return default(ushort);
}
public static ushort ToUInt16OrDefault(this string s, ushort defaultValue )
{
ushort result;
if(ushort.TryParse(s,out result))
{
return result;
}
return defaultValue;
}
public static ushort ToUInt16OrDefault(this string s, NumberStyles numberStyles, IFormatProvider formatProvider, ushort defaultValue)
{
ushort result;
if(ushort.TryParse(s,numberStyles, formatProvider,out result))
{
return result;
}
return defaultValue;
}
public static int ToInt32OrDefault(this string s)
{
int result;
if(int.TryParse(s,out result))
{
return result;
}
return default(int);
}
public static int ToInt32OrDefault(this string s, int defaultValue )
{
int result;
if(int.TryParse(s,out result))
{
return result;
}
return defaultValue;
}
public static int ToInt32OrDefault(this string s, NumberStyles numberStyles, IFormatProvider formatProvider, int defaultValue)
{
int result;
if(int.TryParse(s,numberStyles, formatProvider,out result))
{
return result;
}
return defaultValue;
}
public static uint ToUInt32OrDefault(this string s)
{
uint result;
if(uint.TryParse(s,out result))
{
return result;
}
return default(uint);
}
public static uint ToUInt32OrDefault(this string s, uint defaultValue )
{
uint result;
if(uint.TryParse(s,out result))
{
return result;
}
return defaultValue;
}
public static uint ToUInt32OrDefault(this string s, NumberStyles numberStyles, IFormatProvider formatProvider, uint defaultValue)
{
uint result;
if(uint.TryParse(s,numberStyles, formatProvider,out result))
{
return result;
}
return defaultValue;
}
public static long ToInt64OrDefault(this string s)
{
long result;
if(long.TryParse(s,out result))
{
return result;
}
return default(long);
}
public static long ToInt64OrDefault(this string s, long defaultValue )
{
long result;
if(long.TryParse(s,out result))
{
return result;
}
return defaultValue;
}
public static long ToInt64OrDefault(this string s, NumberStyles numberStyles, IFormatProvider formatProvider, long defaultValue)
{
long result;
if(long.TryParse(s,numberStyles, formatProvider,out result))
{
return result;
}
return defaultValue;
}
public static ulong ToUInt64OrDefault(this string s)
{
ulong result;
if(ulong.TryParse(s,out result))
{
return result;
}
return default(ulong);
}
public static ulong ToUInt64OrDefault(this string s, ulong defaultValue )
{
ulong result;
if(ulong.TryParse(s,out result))
{
return result;
}
return defaultValue;
}
public static ulong ToUInt64OrDefault(this string s, NumberStyles numberStyles, IFormatProvider formatProvider, ulong defaultValue)
{
ulong result;
if(ulong.TryParse(s,numberStyles, formatProvider,out result))
{
return result;
}
return defaultValue;
}
public static float ToFloatOrDefault(this string s)
{
float result;
if(float.TryParse(s,out result))
{
return result;
}
return default(float);
}
public static float ToFloatOrDefault(this string s, float defaultValue )
{
float result;
if(float.TryParse(s,out result))
{
return result;
}
return defaultValue;
}
public static float ToFloatOrDefault(this string s, NumberStyles numberStyles, IFormatProvider formatProvider, float defaultValue)
{
float result;
if(float.TryParse(s,numberStyles, formatProvider,out result))
{
return result;
}
return defaultValue;
}
public static double ToDoubleOrDefault(this string s)
{
double result;
if(double.TryParse(s,out result))
{
return result;
}
return default(double);
}
public static double ToDoubleOrDefault(this string s, double defaultValue )
{
double result;
if(double.TryParse(s,out result))
{
return result;
}
return defaultValue;
}
public static double ToDoubleOrDefault(this string s, NumberStyles numberStyles, IFormatProvider formatProvider, double defaultValue)
{
double result;
if(double.TryParse(s,numberStyles, formatProvider,out result))
{
return result;
}
return defaultValue;
}
public static decimal ToDecimalOrDefault(this string s)
{
decimal result;
if(decimal.TryParse(s,out result))
{
return result;
}
return default(decimal);
}
public static decimal ToDecimalOrDefault(this string s, decimal defaultValue )
{
decimal result;
if(decimal.TryParse(s,out result))
{
return result;
}
return defaultValue;
}
public static decimal ToDecimalOrDefault(this string s, NumberStyles numberStyles, IFormatProvider formatProvider, decimal defaultValue)
{
decimal result;
if(decimal.TryParse(s,numberStyles, formatProvider,out result))
{
return result;
}
return defaultValue;
}
public static DateTime ToDateTimeOrDefault(this string s)
{
DateTime result;
if(DateTime.TryParse(s,out result))
{
return result;
}
return default(DateTime);
}
public static DateTime ToDateTimeOrDefault(this string s, DateTime defaultValue )
{
DateTime result;
if(DateTime.TryParse(s,out result))
{
return result;
}
return defaultValue;
}
public static DateTime ToDateTimeOrDefault(this string s, DateTimeStyles dateTimeStyles, IFormatProvider formatProvider, DateTime defaultValue)
{
DateTime result;
if(DateTime.TryParse(s, formatProvider, dateTimeStyles, out result))
{
return result;
}
return defaultValue;
}
public static bool ToBoolOrDefault(this string s)
{
bool result;
if(bool.TryParse(s,out result))
{
return result;
}
return default(bool);
}
public static bool ToBoolOrDefault(this string s, bool defaultValue )
{
bool result;
if(bool.TryParse(s,out result))
{
return result;
}
return defaultValue;
}
}
}
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Globalization" #>
<#@ output extension=".cs" #>
<#
KeyValuePair<string,string>[] types = new []
{
new KeyValuePair<string,string>("byte","Byte"),
new KeyValuePair<string,string>("short","Int16"),
new KeyValuePair<string,string>("ushort","UInt16"),
new KeyValuePair<string,string>("int","Int32"),
new KeyValuePair<string,string>("uint","UInt32"),
new KeyValuePair<string,string>("long","Int64"),
new KeyValuePair<string,string>("ulong","UInt64"),
new KeyValuePair<string,string>("float","Float"),
new KeyValuePair<string,string>("double","Double"),
new KeyValuePair<string,string>("decimal","Decimal"),
new KeyValuePair<string,string>("DateTime","DateTime"),
new KeyValuePair<string,string>("bool","Bool")
};
#>
using System;
using System.Linq;
using System.Text;
using System.Globalization;
namespace TextDataUtility
{
public static partial class StringExtensions
{
<# foreach(var typeName in types){ #>
public static <#= typeName.Key #> To<#= typeName.Value #>OrDefault(this string s)
{
<#= typeName.Key #> result;
if(<#= typeName.Key #>.TryParse(s,out result))
{
return result;
}
return default(<#= typeName.Key #>);
}
public static <#= typeName.Key #> To<#= typeName.Value #>OrDefault(this string s, <#= typeName.Key #> defaultValue )
{
<#= typeName.Key #> result;
if(<#= typeName.Key #>.TryParse(s,out result))
{
return result;
}
return defaultValue;
}
<# if(typeName.Key == "DateTime")
{ #>
public static <#= typeName.Key #> To<#= typeName.Value #>OrDefault(this string s, DateTimeStyles dateTimeStyles, IFormatProvider formatProvider, <#= typeName.Key #> defaultValue)
{
<#= typeName.Key #> result;
if(<#= typeName.Key #>.TryParse(s, formatProvider, dateTimeStyles, out result))
{
return result;
}
return defaultValue;
}
<# }else if(typeName.Key != "bool")
{ #>
public static <#= typeName.Key #> To<#= typeName.Value #>OrDefault(this string s, NumberStyles numberStyles, IFormatProvider formatProvider, <#= typeName.Key #> defaultValue)
{
<#= typeName.Key #> result;
if(<#= typeName.Key #>.TryParse(s,numberStyles, formatProvider,out result))
{
return result;
}
return defaultValue;
}
<# } #>
<# } #>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment