欧美日韩一区二区三区四区不卡,日韩欧美视频一区二区三区四区,久久精品欧美一区二区三区不卡,国产精品久久久乱弄

咨詢電話:186 7916 6165 咨詢電話:186 7916 6165 (微信同號)    在線QQ:181796286
NEWS BLOG ·
學無止境
關注開優網絡 關注前沿
ASP.NET公共類庫之字符串實用類StringUtil.cs
ASP.NET公共類庫之用戶實用類User.cs

ASP.NET公共類庫之類型轉換類TypeParse.cs

發表日期:2015-09-07    文章編輯:南昌開優網絡    瀏覽次數:3776    標簽:ASP.NET應用

/**********************************************
* 類作用:   類型轉換類
* 作者:開優網絡
* http://www.568387.com
 ***********************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Svnhost.Common
{
    public class TypeParse
    {
        /// <summary>
        /// string型轉換為bool型
        /// </summary>
        /// <param name="strValue">要轉換的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>轉換后的bool類型結果</returns>
        public static bool StrToBool(object Expression, bool defValue)
        {
            if (Expression != null)
            {
                if (string.Compare(Expression.ToString(), "true", true) == 0)
                {
                    return true;
                }
                else if (string.Compare(Expression.ToString(), "false", true) == 0)
                {
                    return false;
                }
            }
            return defValue;
        }

        /// <summary>
        /// 將對象轉換為Int32類型
        /// </summary>
        /// <param name="strValue">要轉換的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>轉換后的int類型結果</returns>
        public static int StrToInt(object Expression, int defValue)
        {

            if (Expression != null)
            {
                string str = Expression.ToString();
                if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*$"))
                {
                    if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
                    {
                        return Convert.ToInt32(str);
                    }
                }
            }
            return defValue;
        }

        /// <summary>
        /// string型轉換為float型
        /// </summary>
        /// <param name="strValue">要轉換的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>轉換后的int類型結果</returns>
        public static float StrToFloat(object strValue, float defValue)
        {
            if ((strValue == null) || (strValue.ToString().Length > 10))
            {
                return defValue;
            }

            float intValue = defValue;
            if (strValue != null)
            {
                bool IsFloat = Regex.IsMatch(strValue.ToString(), @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (IsFloat)
                {
                    intValue = Convert.ToSingle(strValue);
                }
            }
            return intValue;
        }

        /// <summary>
        /// 將long型數值轉換為Int32類型
        /// </summary>
        /// <param name="objNum"></param>
        /// <returns></returns>
        public static int SafeInt32(object objNum)
        {
            if (objNum == null)
            {
                return 0;
            }
            string strNum = objNum.ToString();
            if (ValidateUtils.IsNumeric(strNum))
            {

                if (strNum.ToString().Length > 9)
                {
                    if (strNum.StartsWith("-"))
                    {
                        return int.MinValue;
                    }
                    else
                    {
                        return int.MaxValue;
                    }
                }
                return Int32.Parse(strNum);
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// int型轉換為string型
        /// </summary>
        /// <returns>轉換后的string類型結果</returns>
        public static string IntToStr(int intValue)
        {
            return Convert.ToString(intValue);
        }
    }
}
主站蜘蛛池模板: 庄浪县| 咸丰县| 西丰县| 湖南省| 兴文县| 将乐县| 瑞安市| 邳州市| 辽阳县| 准格尔旗| 迭部县| 南皮县| 大同县| 石景山区| 罗甸县| 大余县| 岑巩县| 荔波县| 南汇区| 安吉县| 华安县| 县级市| 台东市| 江门市| 米易县| 潼南县| 丹江口市| 深圳市| 定边县| 定远县| 宝丰县| 九龙县| 梁山县| 右玉县| 连云港市| 盈江县| 综艺| 灵寿县| 叙永县| 洞口县| 莱芜市|