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

namespace HySoft.Common
{
    public static class StringHelper
    {
        /// <summary>
        /// 截取字符串，超出部分用…
        /// </summary>
        /// <param name="sString"></param>
        /// <param name="nLeng"></param>
        /// <returns></returns>
        public static string SubStr(string sString, int nLeng)
        {
            if (sString.Length <= nLeng)
            {
                return sString;
            }
            else
            {
                return sString.Substring(0, nLeng) + "…";
            }
        }
       /// <summary>
       /// 截取字符串
       /// </summary>
       /// <param name="sString"></param>
       /// <param name="nLeng"></param>
       /// <param name="a">区别第一种方法，随便一个数字都可以</param>
       /// <returns></returns>
        public static string SubStr(string sString, int nLeng,int a)
        {
            if (sString.Length <= nLeng)
            {
                return sString;
            }
            else
            {
                return sString.Substring(0, nLeng);
            }
        }
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="sString"></param>
        /// <param name="nLeng"></param>
        /// <param name="replaceStr">用什么字符串替换删掉的部分</param>
        /// <returns></returns>
        public static string SubStr(string sString, int nLeng, string replaceStr)
        {
            if (sString.Length <= nLeng)
            {
                return sString;
            }
            else
            {
                return sString.Substring(0, nLeng) + replaceStr;
            }
        }
        /// <summary>
        /// 清除脚本
        /// </summary>
        /// <param name="Htmlstring"></param>
        /// <returns></returns>
        public static string NoHTML(string Htmlstring)
        {
            //删除脚本  
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML  
            Htmlstring = Regex.Replace(Htmlstring, @"<.*?>|&.{4,5}", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
            Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
            return Htmlstring;
        }


        /// <summary>
        /// 构造空格
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static string getspace(int i)
        {
            return getstr(i, "　");
        }

        /// <summary>
        /// 构造字符窜
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static string getstr(int i, string str)
        {
            string str1 = "";
            for (int j = 0; j < i; j++)
            {
                str1 += str;
            }
            return str;
        }

        /// <summary>
        /// 清除html标记后截取
        /// </summary>
        /// <param name="sString"></param>
        /// <param name="nLeng"></param>
        /// <returns></returns>
        public static string noHtmlSubStr(string sString, int nLeng)
        {
            string str = NoHTML(sString);
            if (str.Length <= nLeng)
            {
                return str;
            }
            else
            {
                return str.Substring(0, nLeng) + "…";
            }
        }

        /// <summary>
        /// 输出图片
        /// </summary>
        /// <param name="width">最大宽度</param>
        /// <param name="url">路径</param>
        /// <returns></returns>
        public static string responImg(int width, string url)
        {
            return "<img onload=\"javascript:if(this.width>" + width + ")this.width=" + width + "\"  src=\"" + url + "\" />";
        }

        /// <summary>
        /// 输出swf
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string responswf(int width, int height, string path)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" width=\"" + width + "\" height=\"" + height + "\">");
            sb.Append("<param name=\"movie\" value=\"" + path + "\" />");
            sb.Append("<param name=\"quality\" value=\"high\" />");
            sb.Append("<param name=\"wmode\" value=\"opaque\" />");
            sb.Append("<embed src=\"" + path + "\" quality=\"high\"  wmode=\"opaque\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"" + width + "\" height=\"" + height + "\"></embed></object>");
            return sb.ToString();
        }


        /// <summary>
        /// 替换字符串中的单引号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ReplaceSingleQuote(this string str)
        {
            if (string.IsNullOrEmpty(str))
                return string.Empty;
            return str.Replace("'","''");
        }

        /// <summary>
        /// 合并数字字符串
        /// </summary>
        /// <param name="list">数字列表</param>
        /// <param name="charu">用什么字符间隔</param>
        /// <returns></returns>
        public static string hebingStr(this List<int> list, string charu)
        {
            StringBuilder sb = new StringBuilder();
            foreach (int str in list)
            {
                sb.Append(str + charu);
            }
            if (sb.Length == 0)
            {
                return string.Empty;
            }
            sb.Length = sb.Length - 1;
            return sb.ToString();
        }

        /// <summary>
        /// 清楚小数点后的0
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ClearDecimal(this string str)
        {
            if (string.IsNullOrEmpty(str))
                return "0";
            str = float.Parse(str).ToString("0.00");
            if (Int32.Parse(str.Substring(str.IndexOf(".") + 1)) == 0)
            {
                return str.Substring(0, str.IndexOf("."));
            }
            return str;
        }
        /// <summary>
        /// 清楚小数点后的0
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ClearDecimalZero(string str)
        {
            if (string.IsNullOrEmpty(str))
                return "0";
            if (str.IndexOf(".") > 0)
            {
                if (Int32.Parse(str.Substring(str.IndexOf(".") + 1)) == 0)
                {
                    return str.Substring(0, str.IndexOf("."));
                }
                for (int i = str.Length - 1; i >= 0; i--)
                {
                    if (str[i] != '0')
                    {
                        break;
                    }
                    else
                    {
                        str = str.Remove(i);
                    }
                }
                return str;
            }
            else
            {
                return str;
            }
        }

        /// <summary>
        /// 汉字转拼音缩写（调用这个）
        /// /// Code By MuseStudio@hotmail.com
        /// /// 2004-11-30/// </summary>/// <param name="str">要转换的汉字字符串</param>
        /// /// <returns>拼音缩写</returns>
        public static string GetPYString(string str)
        {
            string tempStr = "";
            foreach (char c in str)
            {
                if ((int)c >= 33 && (int)c <= 126)
                {//字母和符号原样保留

                    tempStr += c.ToString();
                }
                else
                {//累加拼音声母     
                    tempStr += GetPYChar(c.ToString());
                }
            }
            return tempStr;
        }
        /// <summary>
        /// /// 取单个字符的拼音声母/// Code By MuseStudio@hotmail.com
        /// /// 2004-11-30/// </summary>/// <param name="c">要转换的单个汉字</param>
        /// /// <returns>拼音声母</returns>
        public static string GetPYChar(string c)
        {
            byte[] array = new byte[2];
            array = System.Text.Encoding.Default.GetBytes(c);
            int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));
            if (i < 0xB0A1) return "*";
            if (i < 0xB0C5) return "a";
            if (i < 0xB2C1) return "b";
            if (i < 0xB4EE) return "c";
            if (i < 0xB6EA) return "d";
            if (i < 0xB7A2) return "e";
            if (i < 0xB8C1) return "f";
            if (i < 0xB9FE) return "g";
            if (i < 0xBBF7) return "h";
            if (i < 0xBFA6) return "j";
            if (i < 0xC0AC) return "k";
            if (i < 0xC2E8) return "l";
            if (i < 0xC4C3) return "m";
            if (i < 0xC5B6) return "n";
            if (i < 0xC5BE) return "o";
            if (i < 0xC6DA) return "p";
            if (i < 0xC8BB) return "q";
            if (i < 0xC8F6) return "r";
            if (i < 0xCBFA) return "s";
            if (i < 0xCDDA) return "t";
            if (i < 0xCEF4) return "w";
            if (i < 0xD1B9) return "x";
            if (i < 0xD4D1) return "y";
            if (i < 0xD7FA) return "z";
            return "*";
        }


        public static int ToInt32(this string str)
        {
            return Convert.ToInt32(str);
        }

        public static decimal Todecimal(this string str)
        {
            return Convert.ToDecimal(str);
        }

        /// <summary>
        /// html编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string HtmlEncode(this string str)
        { 
           return HttpUtility.HtmlEncode(str);
        }

        /// <summary>
        /// Hmtl解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string HtmlDecode(this string str)
        {
            return HttpUtility.HtmlDecode(str);
        }

        /// <summary>
        /// url编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UrlEncode(this string str)
        {
            return HttpUtility.UrlEncode(str);
        }

        /// <summary>
        /// url解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UrlDecode(this string str)
        {
            return HttpUtility.UrlDecode(str);
        }

        public static string GetGuid()
        {
            return Guid.NewGuid().ToString().Replace("-","");
        }
        /// <summary>
        /// 清楚字符串中所有空格，包括\r\n\t
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string CleanSpace(this string str)
        {
            return Regex.Replace(str, @"\s", "");
        }
    }
}
