C#扩展方法类库StringExtensions

101 篇文章 1 订阅
60 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Reflection;
using ECS.Utility;

public static class StringExtensions
{
    public static void BindEnumList(this CheckBoxList ddl, Type obj)
    {
        if (!obj.IsEnum)
            throw new Exception("value not enum!");

        var itemArr = Enum.GetValues(obj);

        foreach (var item in itemArr)
        {
            ddl.Items.Add(new ListItem(item.ToString(), ((int)item).ToString()));
        }
    }


    public static void BindEnumList(this DropDownList ddl, Type obj)
    {
        if (!obj.IsEnum)
            throw new Exception("value not enum!");

        var itemArr = Enum.GetValues(obj);

        foreach (var item in itemArr)
        {
            ddl.Items.Add(new ListItem(item.ToString(), ((int)item).ToString()));
        }
    }


    public static void BindEnumDescriptionList(this DropDownList ddl, Type obj)
    {
        if (!obj.IsEnum)
        {
            throw new ArgumentException("enumItem requires a Enum ");
        }

        var itemArr = Enum.GetValues(obj);
        string[] names = Enum.GetNames(obj);
        FieldInfo fieldInfo;
        object[] attributes;
        DescriptionAttribute descriptionAttribute;


        foreach (string name in names)
        {
            fieldInfo = obj.GetField(name);
            attributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            var value = (int)fieldInfo.GetValue(typeof(string));
            if (attributes.Length > 0)
            {
                descriptionAttribute = attributes.First() as DescriptionAttribute;
                if (descriptionAttribute != null)
                {
                    ddl.Items.Add(new ListItem(descriptionAttribute.Description, value.ToString()));
                }
            }
        }
    }



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



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

    public static int? ToNullableInt(this string value)
    {
        int result;

        if (string.IsNullOrEmpty(value) || !int.TryParse(value, out result))
        {
            return null;
        }

        return result;
    }

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

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

    public static decimal ToRoundDecimal(this string value, decimal defaultValue, int decimals)
    {
        var result = defaultValue;
        result = Math.Round(decimal.TryParse(value, out result) ? result : defaultValue, decimals);
        return result;
    }


    public static decimal? ToNullableDecimal(this string value)
    {
        decimal result;
        if (string.IsNullOrEmpty(value) || !decimal.TryParse(value, out result))
        {
            return null;
        }
        return result;
    }

    public static short? ToNullableShort(this string value)
    {
        short result;

        if (string.IsNullOrEmpty(value) || !short.TryParse(value, out result))
        {
            return null;
        }

        return result;
    }

    public static DateTime? ToNullableDateTime(this string value)
    {
        DateTime result;

        if (DateTime.TryParse(value, out result))
        {
            return result;
        }

        return null;
    }

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

    public static byte? ToNullableByte(this string value)
    {
        byte result;

        if (string.IsNullOrEmpty(value) || !byte.TryParse(value, out result))
        {
            return null;
        }

        return result;
    }


    public static bool? ToNullableBool(this string value)
    {
        bool result;

        if (string.IsNullOrEmpty(value) || !bool.TryParse(value, out result))
        {
            return null;
        }

        return result;
    }

    public static bool ToBool(this string value)
    {
        return bool.Parse(value);
    }

    /// <summary>
    /// 去掉字符串中的html
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string ToNoHtmlString(this string value)
    {
        return Util.StripHTML(value).Trim();
    }



}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值