C#扩展方法类库StringExtensions

简介: using System;using System.Collections.Generic;using System.Linq;using System.
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();
    }



}


 

目录
相关文章
|
4月前
|
算法 C#
C#开源实用的工具类库,集成超过1000多种扩展方法
C#开源实用的工具类库,集成超过1000多种扩展方法
|
5月前
|
XML 移动开发 前端开发
C#HtmlAgilityPack类库再回顾
C#HtmlAgilityPack类库再回顾
42 0
|
11月前
|
C# 开发者
C#扩展方法和工具类的区别
扩展方法和工具类的主要区别在于它们的作用。扩展方法旨在扩展现有的类,而工具类旨在提供一组通用且可靠的方法来执行某些任务。当需要扩展现有的类时,使用扩展方法;当需要实用函数来执行通用任务时,使用工具类。
69 0
|
编译器 C#
c# 自定义扩展方法
c# 自定义扩展方法
|
C#
56【WinForm】WinForm创建类库项目,并同时在项目中调用类库文件C#
【WinForm】WinForm创建类库项目,并同时在项目中调用类库文件C#
198 0
|
存储 安全 API
10分钟学会Visual Studio将自己创建的类库打包到NuGet进行引用(net,net core,C#)
10分钟学会Visual Studio将自己创建的类库打包到NuGet进行引用(net,net core,C#)
|
C#
c#中的扩展方法
c#中的扩展方法
85 0
|
C# 图形学 C++
Unity与 DLL文件 ☀️| 怎样使用 C# 类库 生成一个DLL文件 并 调用!
📢前言 🎬生成DLL文件 🎥使用 C#类库 将Unity中的脚本打包成 DLL文件 并调用 🏳️‍🌈第一步:打开Visual Studio之后,新建一个项目 🏳️‍🌈第二步:选择类库(.NET Framework),改个名字,选择一个位置路径 🏳️‍🌈第三步:然后在创建的脚本中简单写一点代码,如下所示 🏳️‍🌈第四步:然后在解决方案资源管理器右键这个脚本 -> 添加 -> 引用 🏳️‍🌈第五步:然后点击浏览,找到Unity安装路径 -> Editor -> Data -> Managed 下的这两个DLL 文件,点击添加!
Unity与 DLL文件 ☀️| 怎样使用 C# 类库 生成一个DLL文件 并 调用!
|
C# C++
C# 创建与引用dll类库项目实战
本文目录 1. 类的访问修饰符 2. 新建类库项目 3. 编辑类代码 4. 编译并生成dll文件 5. 调用该dll
357 0
C# 创建与引用dll类库项目实战