LoginSignup
9
9

More than 5 years have passed since last update.

.NETでのstringの型変換拡張メソッド

Last updated at Posted at 2015-03-17

string型の値を他の型(intとかboolとか)に変換する拡張メソッドです。

基本、変換エラーが起こるようにしてありますが、
既定値を渡すことで変換ミスが起こったときの保険をかけることも出来ます。

public static class StringExtensions
{
    public static bool ToBoolean(this string value)
    {
        return bool.Parse(value);
    }

    public static bool ToBoolean(this string value, bool defaultValue)
    {
        bool result;
        return bool.TryParse(value, out result) ? result : defaultValue;
    }

    public static short ToInt16(this string value)
    {
        return short.Parse(value);
    }

    public static short ToInt16(this string value, short defaultValue)
    {
        short result;
        return short.TryParse(value, out result) ? result : defaultValue;
    }

    public static int ToInt32(this string value)
    {
        return int.Parse(value);
    }

    public static int ToInt32(this string value, int defaultValue)
    {
        int result;
        return int.TryParse(value, out result) ? result : defaultValue;
    }

    public static long ToInt64(this string value)
    {
        return long.Parse(value);
    }

    public static long ToInt64(this string value, long defaultValue)
    {
        long result;
        return long.TryParse(value, out result) ? result : defaultValue;
    }

    public static double ToDouble(this string value)
    {
        return double.Parse(value);
    }

    public static double ToDouble(this string value, double defaultValue)
    {
        double result;
        return double.TryParse(value, out result) ? result : defaultValue;
    }

    public static float ToSingle(this string value)
    {
        return float.Parse(value);
    }

    public static float ToSingle(this string value, float defaultValue)
    {
        float result;
        return float.TryParse(value, out result) ? result : defaultValue;
    }

    public static decimal ToDecimal(this string value)
    {
        return decimal.Parse(value);
    }

    public static decimal ToDecimal(this string value, decimal defaultValue)
    {
        decimal result;
        return decimal.TryParse(value, out result) ? result : defaultValue;
    }

    public static char ToChar(this string value)
    {
        return char.Parse(value);
    }

    public static char ToChar(this string value, char defaultValue)
    {
        char result;
        return char.TryParse(value, out result) ? result : defaultValue;
    }

    public static DateTime ToDateTime(this string value)
    {
        return DateTime.Parse(value);
    }

    public static DateTime ToDateTime(this string value, DateTime defaultValue)
    {
        DateTime result;
        return DateTime.TryParse(value, out result) ? result : defaultValue;
    }

    public static T ToEnum<T>(this string value) where T : struct
    {
        return (T)Enum.Parse(typeof(T), value);
    }

    public static T ToEnum<T>(this string value, T defaultValue) where T : struct
    {
        T result;
         return Enum.TryParse(value, out result) ? result : defaultValue;
    }
}



実際に使うとこんなカンジです。

class Program
{
    static void Main(string[] args)
    {
        const string error = "asdf85g74";

        const string b = "true";
        Console.WriteLine(b.ToBoolean());
        Console.WriteLine("false".ToBoolean());
        Console.WriteLine(error.ToBoolean(true));

        const string s = "10";
        Console.WriteLine(s.ToInt16());
        Console.WriteLine("-10".ToInt16());
        Console.WriteLine(error.ToInt16(short.MinValue));

        const string i = "1000000";
        Console.WriteLine(i.ToInt32());
        Console.WriteLine("-1000000".ToInt32());
        Console.WriteLine(error.ToInt32(int.MinValue));

        const string l = "10000000000";
        Console.WriteLine(l.ToInt64());
        Console.WriteLine("-10000000000".ToInt64());
        Console.WriteLine(error.ToInt64(long.MinValue));

        const string f = "100.0";
        Console.WriteLine(f.ToSingle());
        Console.WriteLine("-100.0".ToSingle());
        Console.WriteLine(error.ToSingle(float.MinValue));

        const string db = "100.0";
        Console.WriteLine(db.ToDouble());
        Console.WriteLine("-100.0".ToDouble());
        Console.WriteLine(error.ToDouble(double.MinValue));

        const string dc = "100.000001";
        Console.WriteLine(dc.ToDecimal());
        Console.WriteLine("-100.000001".ToDecimal());
        Console.WriteLine(error.ToDecimal(decimal.MinValue));

        const string dt = "2014/09/01 0:00:00";
        Console.WriteLine(dt.ToDateTime());
        Console.WriteLine("2014/12/31 23:59:59".ToDateTime());
        Console.WriteLine(error.ToDateTime(DateTime.Now));

        const string e = "Sales";
        Console.WriteLine(e.ToEnum<Category>());
        Console.WriteLine("Develop".ToEnum<Category>());
        Console.WriteLine("3".ToEnum<Category>()); // 数値の文字列変換も可能
        Console.WriteLine(error.ToEnum(Category.All));

        Console.ReadLine();
    }
}

public enum Category
{
    All = 0,
    Sales = 1,
    Develop = 2,
    Support = 3
}



結果は以下の通り。

result.jpg


自分が書いたコードでこれらを使わなきゃいけないケースで滅多にないはずで、
もし使わなきゃいけない状況になったらクラス設計などを見直してみた方が良いと思います。

実際の使いどころとしては外部APIの戻り値がstring型とか、
アプリケーション側でどうしてもコントロールできないところではないでしょうか。

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9