C#常用方法集合

 public class Utility:Page
    {

        #region 数据转换
        /// <summary>
        /// 返回对象obj的String值,obj为null时返回空值。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>字符串。</returns>
        public static string ToObjectString(object obj)
        {
            return null == obj ? String.Empty : obj.ToString();
        }

        /// <summary>
        /// 将对象转换为数值(Int32)类型,转换失败返回-1。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>Int32数值。</returns>
        public static int ToInt(object obj)
        {
            try
            {
                return int.Parse(ToObjectString(obj));
            }
            catch
            { return -1; }
        }

        /// <summary>
        /// 将对象转换为数值(Int32)类型。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <param name="returnValue">转换失败返回该值。</param>
        /// <returns>Int32数值。</returns>
        public static int ToInt(object obj,int returnValue)
        {
            try
            {
                return int.Parse(ToObjectString(obj));
            }
            catch
            { return returnValue; }
        }
        /// <summary>
        /// 将对象转换为数值(Long)类型,转换失败返回-1。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>Long数值。</returns>
        public static long ToLong(object obj)
        {
            try
            {
                return long.Parse(ToObjectString(obj));
            }
            catch
            { return -1L; }
        }
        /// <summary>
        /// 将对象转换为数值(Long)类型。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <param name="returnValue">转换失败返回该值。</param>
        /// <returns>Long数值。</returns>
        public static long ToLong(object obj,long returnValue)
        {
            try
            {
                return long.Parse(ToObjectString(obj));
            }
            catch
            { return returnValue; }
        }
        /// <summary>
        /// 将对象转换为数值(Decimal)类型,转换失败返回-1。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>Decimal数值。</returns>
        public static decimal ToDecimal(object obj)
        {
            try
            {
                return decimal.Parse(ToObjectString(obj));
            }
            catch
            { return -1M; }
        }

        /// <summary>
        /// 将对象转换为数值(Decimal)类型。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <param name="returnValue">转换失败返回该值。</param>
        /// <returns>Decimal数值。</returns>
        public static decimal ToDecimal(object obj,decimal returnValue)
        {
            try
            {
                return decimal.Parse(ToObjectString(obj));
            }
            catch
            { return returnValue; }
        }
        /// <summary>
        /// 将对象转换为数值(Double)类型,转换失败返回-1。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>Double数值。</returns>
        public static double ToDouble(object obj)
        {
            try
            {
                return double.Parse(ToObjectString(obj));
            }
            catch
            { return -1; }
        }

        /// <summary>
        /// 将对象转换为数值(Double)类型。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <param name="returnValue">转换失败返回该值。</param>
        /// <returns>Double数值。</returns>
        public static double ToDouble(object obj,double returnValue)
        {
            try
            {
                return double.Parse(ToObjectString(obj));
            }
            catch
            { return returnValue; }
        }
        /// <summary>
        /// 将对象转换为数值(Float)类型,转换失败返回-1。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>Float数值。</returns>
        public static float ToFloat(object obj)
        {
            try
            {
                return float.Parse(ToObjectString(obj));
            }
            catch
            { return -1; }
        }

        /// <summary>
        /// 将对象转换为数值(Float)类型。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <param name="returnValue">转换失败返回该值。</param>
        /// <returns>Float数值。</returns>
        public static float ToFloat(object obj,float returnValue)
        {
            try
            {
                return float.Parse(ToObjectString(obj));
            }
            catch
            { return returnValue; }
        }
        /// <summary>
        /// 将对象转换为数值(DateTime)类型,转换失败返回Now。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>DateTime值。</returns>
        public static DateTime ToDateTime(object obj)
        {
            try
            {
                DateTime dt = DateTime.Parse(ToObjectString(obj));
                if( dt > DateTime.MinValue && DateTime.MaxValue > dt)
                    return dt;
                return DateTime.Now;
            }
            catch
            { return DateTime.Now; }
        }

        /// <summary>
        /// 将对象转换为数值(DateTime)类型。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <param name="returnValue">转换失败返回该值。</param>
        /// <returns>DateTime值。</returns>
        public static DateTime ToDateTime(object obj,DateTime returnValue)
        {
            try
            {
                DateTime dt = DateTime.Parse(ToObjectString(obj));
                if( dt > DateTime.MinValue && DateTime.MaxValue > dt)
                    return dt;
                return returnValue;
            }
            catch
            { return returnValue; }
        }
        /// <summary>
        /// 从Boolean转换成byte,转换失败返回0。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>Byte值。</returns>
        public static byte ToByteByBool( object obj )
        {
            string text = ToObjectString(obj).Trim();
            if( text == string.Empty)
                return 0;
            else
            {
                try
                {
                    return (byte)(text.ToLower()=="true"?1:0);
                }
                catch
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// 从Boolean转换成byte。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <param name="returnValue">转换失败返回该值。</param>
        /// <returns>Byte值。</returns>
        public static byte ToByteByBool( object obj, byte returnValue )
        {
            string text = ToObjectString(obj).Trim();
            if( text == string.Empty)
                return returnValue;
            else
            {
                try
                {
                    return (byte)(text.ToLower()=="true"?1:0);
                }
                catch
                {
                    return returnValue;
                }
            }
        }
        /// <summary>
        /// 从byte转换成Boolean,转换失败返回false。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>Boolean值。</returns>
        public static bool ToBoolByByte( object obj )
        {
            try
            {
                string s = ToObjectString(obj).ToLower();
                return s == "1" || s== "true"?true:false;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 从byte转换成Boolean。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <param name="returnValue">转换失败返回该值。</param>
        /// <returns>Boolean值。</returns>
        public static bool ToBoolByByte( object obj, bool returnValue )
        {
            try
            {
                string s = ToObjectString(obj).ToLower();
                return s == "1" || s== "true"?true:false;
            }
            catch
            {
                return returnValue;
            }
        }
        #endregion

        #region 数据判断
        /// <summary>
        /// 判断文本obj是否为空值。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>Boolean值。</returns>
        public static bool IsEmpty(string obj)
        {
            return ToObjectString(obj).Trim() == String.Empty ? true : false;
        }

        /// <summary>
        /// 判断对象是否为正确的日期值。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>Boolean。</returns>
        public static bool IsDateTime(object obj)
        {
            try
            {
                DateTime dt = DateTime.Parse(ToObjectString(obj));
                if( dt > DateTime.MinValue && DateTime.MaxValue > dt)
                    return true;
                return false;
            }
            catch
            { return false; }
        }

        /// <summary>
        /// 判断对象是否为正确的Int32值。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>Int32值。</returns>
        public static bool IsInt(object obj)
        {
            try
            {
                int.Parse(ToObjectString(obj));
                return true;
            }
            catch
            { return false; }
        }

        /// <summary>
        /// 判断对象是否为正确的Long值。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>Long值。</returns>
        public static bool IsLong(object obj)
        {
            try
            {
                long.Parse(ToObjectString(obj));
                return true;
            }
            catch
            { return false; }
        }

        /// <summary>
        /// 判断对象是否为正确的Float值。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>Float值。</returns>
        public static bool IsFloat(object obj)
        {
            try
            {
                float.Parse(ToObjectString(obj));
                return true;
            }
            catch
            { return false; }
        }

        /// <summary>
        /// 判断对象是否为正确的Double值。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>Double值。</returns>
        public static bool IsDouble(object obj)
        {
            try
            {
                double.Parse(ToObjectString(obj));
                return true;
            }
            catch
            { return false; }
        }

        /// <summary>
        /// 判断对象是否为正确的Decimal值。
        /// </summary>
        /// <param name="obj">对象。</param>
        /// <returns>Decimal值。</returns>
        public static bool IsDecimal(object obj)
        {
            try
            {
                decimal.Parse(ToObjectString(obj));
                return true;
            }
            catch
            { return false; }
        }
        #endregion

        #region 数据操作
        /// <summary>
        /// 去除字符串的所有空格。
        /// </summary>
        /// <param name="text">字符串</param>
        /// <returns>字符串</returns>
        public static string StringTrimAll( string text )
        {
            string _text = ToObjectString(text);
            string returnText = String.Empty;
            char[] chars = _text.ToCharArray();
            for( int i=0;i<chars.Length;i++)
            {
                if( chars[i].ToString() != string.Empty)
                    returnText += chars[i].ToString();
            }
            return returnText;
        }

        /// <summary>
        /// 去除数值字符串的所有空格。
        /// </summary>
        /// <param name="numricString">数值字符串</param>
        /// <returns>String</returns>
        public static string NumricTrimAll( string numricString )
        {
            string text = ToObjectString(numricString).Trim();
            string returnText = String.Empty;
            char[] chars = text.ToCharArray();
            for( int i=0;i<chars.Length;i++)
            {
                if( chars[i].ToString() == "+" || chars[i].ToString() == "-" || IsDouble( chars[i].ToString()) )
                    returnText += chars[i].ToString();
            }
            return returnText;
        }

        /// <summary>
        /// 在数组中查找匹配对象类型
        /// </summary>
        /// <param name="array">数组</param>
        /// <param name="obj">对象</param>
        /// <returns>Boolean</returns>
        public static bool ArrayFind(Array array,object obj)
        {
            bool b = false;
            foreach(object obj1 in array)
            {
                if(obj.Equals(obj1))
                {
                    b = true;
                    break;
                }
            }
            return b;
        }

        /// <summary>
        /// 在数组中查找匹配字符串
        /// </summary>
        /// <param name="array">数组</param>
        /// <param name="obj">对象</param>
        /// <param name="unUpLower">是否忽略大小写</param>
        /// <returns>Boolean</returns>
        public static bool ArrayFind(Array array,string obj,bool unUpLower)
        {
            bool b = false;
            foreach(string obj1 in array)
            {
                if(!unUpLower)
                {
                    if(obj.Trim().Equals(obj1.ToString().Trim()))
                    {
                        b = true;
                        break;
                    }
                }
                else
                {
                    if(obj.Trim().ToUpper().Equals(obj1.ToString().Trim().ToUpper()))
                    {
                        b = true;
                        break;
                    }
                }
            }
            return b;
        }
        /// <summary>
        /// 替换字符串中的单引号。
        /// </summary>
        /// <param name="inputString">字符串</param>
        /// <returns>String</returns>
        public static string ReplaceInvertedComma( string inputString )
        {
            return inputString.Replace("‘","‘‘");
        }

        /// <summary>
        /// 判断两个字节数组是否具有相同值.
        /// </summary>
        /// <param name="bytea">字节1</param>
        /// <param name="byteb">字节2</param>
        /// <returns>Boolean</returns>
        public static bool CompareByteArray(byte[] bytea,byte[] byteb)
        {
            if(null == bytea || null == byteb)
            {
                return false;
            }
            else
            {
                int aLength = bytea.Length;
                int bLength = byteb.Length;
                if(aLength != bLength)
                    return false;
                else
                {
                    bool compare = true;
                    for(int index = 0; index < aLength; index++)
                    {
                        if(bytea[index].CompareTo(byteb[index]) != 0)
                        {
                            compare = false;
                            break;
                        }
                    }
                    return compare;
                }
            }
        }

        /// <summary>
        /// 日期智能生成。
        /// </summary>
        /// <param name="inputText">字符串</param>
        /// <returns>DateTime</returns>
        public static string BuildDate( string inputText )
        {
            try
            {
                return DateTime.Parse( inputText ).ToShortDateString();
            }
            catch
            {
                string text = NumricTrimAll( inputText );
                string year = DateTime.Now.Year.ToString();
                string month = DateTime.Now.Month.ToString();
                string day = DateTime.Now.Day.ToString();
                int length = text.Length;
                if( length == 0 )
                    return String.Empty;
                else
                {
                    if( length<=2 )
                        day = text;
                    else if( length<=4 )
                    {
                        month = text.Substring(0,2);
                        day = text.Substring(2,length-2);
                    }
                    else if( length<=6 )
                    {
                        year = text.Substring(0,4);
                        month = text.Substring(4,length-4);
                    }
                    else if( length>6)
                    {
                        year = text.Substring(0,4);
                        month = text.Substring(4,2);
                        day = text.Substring(6,length-6);
                    }
                    try
                    {
                        return DateTime.Parse( year+"-"+month+"-"+day ).ToShortDateString();
                    }
                    catch
                    {
                        return String.Empty;
                    }
                }
            }
        }

        /// <summary>
        /// 检查文件是否真实存在。
        /// </summary>
        /// <param name="path">文件全名(包括路径)。</param>
        /// <returns>Boolean</returns>
        public static bool IsFileExists(string path)
        {
            try
            {
                return File.Exists(path);
            }
            catch
            {   return false; }
        }

        /// <summary>
        /// 检查目录是否真实存在。
        /// </summary>
        /// <param name="path">目录路径.</param>
        /// <returns>Boolean</returns>
        public static bool IsDirectoryExists(string path)
        {
            try
            {
                return Directory.Exists(Path.GetDirectoryName(path));
            }
            catch
            {   return false; }
        }

        /// <summary>
        /// 查找文件中是否存在匹配行。
        /// </summary>
        /// <param name="fi">目标文件.</param>
        /// <param name="lineText">要查找的行文本.</param>
        /// <param name="lowerUpper">是否区分大小写.</param>
        /// <returns>Boolean</returns>
        public static bool FindLineTextFromFile(FileInfo fi,string lineText,bool lowerUpper)
        {
            bool b = false;
            try
            {
                if(fi.Exists)
                {
                    StreamReader sr=new StreamReader(fi.FullName);
                    string g = "";
                    do
                    {
                        g=sr.ReadLine();
                        if(lowerUpper)
                        {
                            if(ToObjectString(g).Trim() == ToObjectString(lineText).Trim())
                            {
                                b = true;
                                break;
                            }
                        }
                        else
                        {
                            if(ToObjectString(g).Trim().ToLower() == ToObjectString(lineText).Trim().ToLower())
                            {
                                b = true;
                                break;
                            }
                        }
                    }
                    while(sr.Peek()!=-1);
                    sr.Close();
                }
            }
            catch
            {   b =false;   }
            return b;
        }

        /// <summary>
        /// 判断父子级关系是否正确。
        /// </summary>
        /// <param name="table">数据表。</param>
        /// <param name="columnName">子键列名。</param>
        /// <param name="parentColumnName">父键列名。</param>
        /// <param name="inputString">子键值。</param>
        /// <param name="compareString">父键值。</param>
        /// <returns></returns>
        public static bool IsRightParent(DataTable table,string columnName,string parentColumnName,string inputString,string compareString)
        {
            ArrayList array = new ArrayList();
            SearchChild(array,table,columnName,parentColumnName,inputString,compareString);
            return array.Count == 0;
        }

        // 内部方法
        private static void SearchChild(ArrayList array,DataTable table,string columnName,string parentColumnName,string inputString,string compareString)
        {
            DataView view = new DataView(table);
            view.RowFilter = parentColumnName+"=‘"+ReplaceInvertedComma(inputString.Trim())+"‘";//找出所有的子类。
            //查找表中的数据的ID是否与compareString相等,相等返回 false;不相等继续迭代。
            for(int index = 0 ;index < view.Count;index ++)
            {
                if(Utility.ToObjectString(view[index][columnName]).ToLower() == compareString.Trim().ToLower())
                {
                    array.Add("1");
                    break;
                }
                else
                {
                    SearchChild(array,table,columnName,parentColumnName,Utility.ToObjectString(view[index][columnName]),compareString);
                }
            }
        }

        #endregion

        #region 日期

        /// <summary>
        /// 格式化日期类型,返回字符串
        /// </summary>
        /// <param name="dtime">日期</param>
        /// <param name="s">日期年月日间隔符号</param>
        /// <returns></returns>
        public static String Fomatdate(DateTime dtime,String s)
        {
            String datestr="";
            datestr=dtime.Year.ToString() + s + dtime.Month.ToString().PadLeft(2,‘0‘)+ s +dtime.Day.ToString().PadLeft(2,‘0‘);
            return datestr;
        }

        /// <summary>
        /// 返回日期差
        /// </summary>
        /// <param name="sdmin">开始日期</param>
        /// <param name="sdmax">结束日期</param>
        /// <returns>日期差:负数为失败</returns>
        public static int Datediff(DateTime sdmin,DateTime sdmax)
        {
            try
            {
                double i=0;
                while(sdmin.AddDays(i)<sdmax)
                {
                    i++;
                }
                return Utility.ToInt(i);
            }
            catch
            {
                return -1;
            }
        }

        /// <summary>
        /// 返回日期差
        /// </summary>
        /// <param name="sdmin">开始日期</param>
        /// <param name="sdmax">结束日期</param>
        /// <returns>日期差:负数为失败</returns>
        public static int Datediff(String sdmin,String sdmax)
        {
            try
            {
                DateTime dmin;
                DateTime dmax;
                dmin=DateTime.Parse(sdmin);
                dmax=DateTime.Parse(sdmax);
                double i=0;
                while(dmin.AddDays(i)<dmax)
                {
                    i++;
                }
                return Utility.ToInt(i);
            }
            catch
            {
                return -1;
            }
        }

        #endregion

        #region 转换用户输入

        /// <summary>
        /// 将用户输入的字符串转换为可换行、替换Html编码、无危害数据库特殊字符、去掉首尾空白、的安全方便代码。
        /// </summary>
        /// <param name="inputString">用户输入字符串</param>
        public static string ConvertStr(string inputString)
        {
            string retVal=inputString;
            //retVal=retVal.Replace("&","&amp;");
            retVal=retVal.Replace("\"","&quot;");
            retVal=retVal.Replace("<","&lt;");
            retVal=retVal.Replace(">","&gt;");
            retVal=retVal.Replace(" ","&nbsp;");
            retVal=retVal.Replace("  ","&nbsp;&nbsp;");
            retVal=retVal.Replace("\t","&nbsp;&nbsp;");
            retVal=retVal.Replace("\r", "<br>");
            return retVal;
        }

        public static string InputText(string inputString)
        {
            string retVal=inputString;
            retVal= ConvertStr(retVal);
            retVal=retVal.Replace("[url]", "");
            retVal=retVal.Replace("[/url]", "");
            return retVal;
        }

        /// <summary>
        /// 将html代码显示在网页上
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        public static string OutputText(string inputString)
        {
            string retVal=System.Web.HttpUtility.HtmlDecode(inputString);
            retVal=retVal.Replace("<br>","");
            retVal=retVal.Replace("&amp","&;");
            retVal=retVal.Replace("&quot;","\"");
            retVal=retVal.Replace("&lt;","<");
            retVal=retVal.Replace("&gt;",">");
            retVal=retVal.Replace("&nbsp;"," ");
            retVal=retVal.Replace("&nbsp;&nbsp;","  ");
            return retVal;
        }

        public static string ToUrl(string inputString)
        {
            string retVal=inputString;
            retVal= ConvertStr(retVal);
            return Regex.Replace(retVal,@"\[url](?<x>[^\]]*)\[/url]",@"<a href=""$1"" target=""_blank"">$1</a>",RegexOptions.IgnoreCase);
        }

        public static string GetSafeCode(string str)
        {
              str=str.Replace("‘","");
              str=str.Replace(char.Parse("34"),‘ ‘);
              str=str.Replace(";","");
            return str;
        }

        #endregion

        //#region 弹出框

        ///// <summary>
        ///// 服务器端弹出alert对话框
        ///// </summary>
        ///// <param name="str_Message">提示信息,例子:"请输入您姓名!"</param>
        ///// <param name="page">Page类</param>
        //public static void Alert(string str_Message,Page page)
        //{
        //    page.RegisterStartupScript("","<script>alert(‘"+str_Message+"‘);</script>");
        //}
        ///// <summary>
        ///// 服务器端弹出alert对话框
        ///// </summary>
        ///// <param name="str_Ctl_Name">获得焦点控件Id值,比如:txt_Name</param>
        ///// <param name="str_Message">提示信息,例子:"请输入您姓名!"</param>
        ///// <param name="page">Page类</param>
        //public static void Alert(string str_Ctl_Name,string str_Message,Page page)
        //{
        //    page.RegisterStartupScript("","<script>alert(‘"+str_Message+"‘);document.forms(0)."+str_Ctl_Name+".focus(); document.forms(0)."+str_Ctl_Name+".select();</script>");
        //}
        ///// <summary>
        ///// 服务器端弹出confirm对话框,该函数有个弊端,必须放到响应事件的最后,目前没有妥善解决方案
        ///// </summary>
        ///// <param name="str_Message">提示信息,例子:"您是否确认删除!"</param>
        ///// <param name="btn">隐藏Botton按钮Id值,比如:btn_Flow</param>
        ///// <param name="page">Page类</param>
        //public static void Confirm(string str_Message,string btn,Page page)
        //{
        //    page.RegisterStartupScript("","<script> if (confirm(‘"+str_Message+"‘)==true){document.forms(0)."+btn+".click();}</script>");
        //}
        ///// <summary>
        /////  服务器端弹出confirm对话框,询问用户准备转向相应操作,包括“确定”和“取消”时的操作
        ///// </summary>
        ///// <param name="str_Message">提示信息,比如:"成功增加数据,单击\"确定\"按钮填写流程,单击\"取消\"修改数据"</param>
        ///// <param name="btn_Redirect_Flow">"确定"按钮id值</param>
        ///// <param name="btn_Redirect_Self">"取消"按钮id值</param>
        ///// <param name="page">Page类</param>
        //public static void Confirm(string str_Message,string btn_Redirect_Flow,string btn_Redirect_Self,Page page)
        //{
        //    page.RegisterStartupScript("","<script> if (confirm(‘"+str_Message+"‘)==true){document.forms(0)."+btn_Redirect_Flow+".click();}else{document.forms(0)."+btn_Redirect_Self+".click();}</script>");
        //}

        //#endregion

        /// <summary>
        /// 设置绑定到DataGrid的DataTable的记录行数,如不够则添加空行
        /// </summary>
        /// <param name="myDataTable">数据表</param>
        /// <param name="intPageCount">DataGrid分页时每页行数</param>
        public static void SetTableRows(DataTable myDataTable,int intPageCount)
        {
            try
            {
                int intTemp=myDataTable.Rows.Count%intPageCount;
                if ((myDataTable.Rows.Count==0) || (intTemp!=0))
                {
                    for(int i=0;i<(intPageCount-intTemp);i++)
                    {
                        DataRow myDataRow=myDataTable.NewRow();
                        myDataTable.Rows.Add(myDataRow);
                    }
                }
            }
            catch
            {
                throw;
            }
        }

        public static string GetGuid(string guid)
        {
            return guid.Replace("-", "");
        }

        public static string ReadConfig(string filePath)
        {
            return System.Configuration.ConfigurationManager.AppSettings[filePath];
        }

        #region   字符串长度区分中英文截取
        /// <summary>
        /// 截取文本,区分中英文字符,中文算两个长度,英文算一个长度
        /// </summary>
        /// <param name="str">待截取的字符串</param>
        /// <param name="length">需计算长度的字符串</param>
        /// <returns>string</returns>
        public static string GetSubString(string str, int length)
        {
            string temp = str;
            int j = 0;
            int k = 0;
            for (int i = 0; i < temp.Length; i++)
            {
                if (Regex.IsMatch(temp.Substring(i, 1), @"[\u4e00-\u9fa5]+"))
                {
                    j += 2;
                }
                else
                {
                    j += 1;
                }
                if (j <= length)
                {
                    k += 1;
                }
                if (j > length)
                {
                    return temp.Substring(0, k) + "...";
                }
            }
            return temp;
        }
        #endregion
    }

时间: 2024-10-13 03:19:43

C#常用方法集合的相关文章

日期操作常用方法集合

记录一些开发中遇到的日期操作. 1.获取距离今天指定日期间隔的日期 1 /** 2 * 获取距离今天指定日期间隔的日期,正数表示往未来的间隔日期,负数表示过去的间隔日期 3 * 返回形式为YYYY-MM-DD格式的日期 4 * @author mmq 2014年11月13日17:38:48 5 * @return String 6 */ 7 public static String getDateByDifferFromToday(int differ){ 8 Calendar c = Cale

java String 常用方法集合

String a = "abc";String b = "abc";a==b ;//返回true,因为a,b指向的是同一个地址 String a = new String("abc");String b = new String("abc");a==b;//返回false,因为a,b指向的不是同一个地址------------------------------------------------------------a =

js中常用方法集合

1. var post = new Array(); post['class'] = '1'; post['type'] = '2'; post['id'] = '3'; var postjson = JSON.stringify(post); console.log(postjson); 打印出来的值是一个[],原因是什么? post new出来的是一个数组对象,然后你给数组对象设置属性,但是数组序列化的时候只会序列化数组中的元素,会忽略对象上的属性和值. 按我实际的需求,直接用一个空的对象来

OC 第七天 集合 lesson7

NSSet 常用方法 集合类似一个罐子,一旦把对象"丢进"NSSet集合,集合里多个对象之间没有明显的顺序. NSSet按Hash算法来存储集合中的元素,因此有很好的存取和查找性能. 1.创建集合对象. <span style="font-size:18px;"> NSSet *nanShen = [NSSet setWithObjects:@"曹大哥", @"david", @"洁哥", @&

Postman-常用方法集合

postman常用方法集合: 1.设置环境变量 postman.setEnvironmentVariable("key", "value"); pm.environment.get("key", "value");//postman 5.0以上版本设置环境变量的方法 2.设置全局变量 postman.setGlobalVariable("key", "value"); pm.global

postman tests常用方法

postman常用方法集合: 1.设置环境变量 postman.setEnvironmentVariable("key", "value"); pm.environment.get("key", "value");//postman 5.0以上版本设置环境变量的方法 2.设置全局变量 postman.setGlobalVariable("key", "value"); pm.global

iOS中 三种随机数方法详解

ios 有如下三种随机数方法: 1 2 3 4 5 6 7 8 9 10 //第一种 srand((unsigned)time(0)); //不加这句每次产生的随机数不变 int i = rand() % 5; //第二种 srandom(time(0)); int i = random() % 5; //第三种 int i = arc4random() % 5 ; 注: ① rand()和random()实际并不是一个真正的伪随机数发生器,在使用之前需要先初始化随机种子,否则每次生成的随机数一

NSSet、NSMutableSet基本用法

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源码下载:点我传送 游戏官方下载:http://dwz.cn/RwTjl 游戏视频预览:http://dwz.cn/RzHHd 游戏开发博客:http://dwz.cn/RzJzI 游戏源码传送:http://dwz.cn/Nret1 在Foundation框架中,提供了NSSet类,它是一组单值对象的集合,且NSSet实例中元素

odoo之ERP系统

p { line-height: 120% } a:link { } odoo大纲 第一部分:数据库postgressql 大象 第二部分:ORM(API) 第三部分:客户端 用python软件写: .py文件 包含两部分:1.自定义部分,由自己写,定义类和功能. 2.继承部分,针对的是原生的模块,继承他原有的功能,添加新的方法. .xml文件 视图部分:最常用的是form视图和tree视图,等等其他视图,还有页签和其他控件按钮 p { line-height: 120% } a:link {