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();
    }

}

C#扩展方法类库StringExtensions的更多相关文章

  1. 开源且功能强大的C# 扩展方法类库Pure.Ext,包含1000+个拓展方法 (支持.Net Framework和.Net Core)

    先上地址 Github: https://github.com/purestackorg/pure.ext Gitee: https://gitee.com/purestack/pure.ext 扩展 ...

  2. C#秘密武器之扩展方法

    原文:C#秘密武器之扩展方法 为何要用扩展方法? 作为一个.NET程序猿,我们经常要跟.net自带类库或者第三方dll类库打交道,有时候我们未必能够通过反编译来查看它们的代码,但是我们通常需要给它们扩 ...

  3. 【原创】开源Math.NET基础数学类库使用(12)C#随机数扩展方法

                   本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新  开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...

  4. 开源Math.NET基础数学类库使用(12)C#随机数扩展方法

    原文:[原创]开源Math.NET基础数学类库使用(12)C#随机数扩展方法                本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p ...

  5. 【开源】OSharp框架解说系列(3):扩展方法

    OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...

  6. .NET 扩展方法 (一)

    我还记得刚刚学编程的时候,老师经常会提到一句话:注意空指针.所以经常在某些“入口”位置,进行代码校验,空指针的判断就是其中的一项工作. string类型作为常用的数据类型,它在项目中出现的机率极高,所 ...

  7. .NET 扩展方法 (二)

    上一篇随笔 .NET 扩展方法 (一) 已经对 扩展方法有了大致的介绍,这篇算是一个补充,让我们来看一下扩展方法的几个细节: 一.扩展方法具有继承性 当使用扩展方法扩展一个类型的时候,其也扩展了派生类 ...

  8. 再谈扩展方法,从string.IsNullOrEmpty()说起

    string.IsNullOrEmpty()这个方法算得上是.net中使用频率最高的方法之一.此方法是string的一个静态方法,类似的静态方法在string这个类中还有很多.那么这样的方法作为静态方 ...

  9. .NET开发中经常用到的扩展方法

    整理一下自己经常用到的几个扩展方法,在实际项目中确实好用,节省了不少的工作量. 1  匿名对象转化 在WinForm中,如果涉及较长时间的操作,我们一般会用一个BackgroundWorker来做封装 ...

随机推荐

  1. 【AIX】AIX内存机制

    [AIX]AIX内存机制 1  虚拟内存 虚拟内存是物理内存和交换空间(Paging Space)组合形成的虚拟内存空间, 通过虚拟的地址空间映射到物理内存或者 Paging Space. 在 AIX ...

  2. CF 375C Circling Round Treasures [DP(spfa) 状压 射线法]

    C - Circling Round Treasures 题意: 在一个$n*m$的地图上,有一些障碍,还有a个宝箱和b个炸弹.你从(sx,sy)出发,走四连通的格子.你需要走一条闭合的路径,可以自交 ...

  3. Windows 动态链接库编程

    Windows 动态链接库编程  1.介绍Windows操作系统是应用最关的操作系统,因此动态链接库也为程序员所熟悉,即使对于普通的使用者来说,很多时候也会碰到.dll结尾的文件,这就是动态链接库文件 ...

  4. 全球(局)唯一标识符GUID的使用

    1.GUID百科介绍: 1.全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) .GUID是 ...

  5. ASCII Art (English)

    Conmajia, 2012 Updated on Feb. 18, 2018 What is ASCII art? It's graphic symbols formed by ASCII char ...

  6. R语言-主成分分析

    1.PCA 使用场景:主成分分析是一种数据降维,可以将大量的相关变量转换成一组很少的不相关的变量,这些无关变量称为主成分 步骤: 数据预处理(保证数据中没有缺失值) 选择因子模型(判断是PCA还是EF ...

  7. Sublime 远程连接 Linux服务器

    Sublime是一款强大的编辑器,它的强大体现在它强大的插件. 要实现Sublime 远程连接 Linux服务器,需要使用插件SFTP. 一. 插件安装 用Package Control安装插件按下C ...

  8. 从此不再担心键盘遮住输入框OC(

    从此不再担心键盘遮住输入框OC(二) 字数544 阅读1492 评论15 喜欢25 在我发布这篇文章没多久之前,我发布了一篇叫 从此不再担心键盘遮住输入框OC(一)的文章.我在那篇文章中介绍了我的键盘 ...

  9. PHP将HTML的内容保存成word文档

    <?php class word { function start() { ob_start(); echo '<html xmlns:o="urn:schemas-micros ...

  10. Centos下安装Lamp和vsftpd、redis

    yum安装httpd和php.mysql服务 yum search httpd //搜索httpd开头的软件包 yum install httpd.x86_64 //找到apache 对应的软件包名 ...