C#中字符串处理的一些自定义方法

原创 2007年09月27日 08:53:00

using System.Collections.Generic;
using System.Linq;

using System;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Web;
using System.Collections;
using System.Net;
using System.IO;

namespace SystemFramework.CommonFunctions
{
    /// <summary>
    /// Summary description for StrManage.
    /// </summary>
    public class StrManage
    {
        public StrManage()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region 是否是带小数的字符
        /// <summary>
        /// 是否是带小数的字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDoubleValid(string str)
        {
            Regex r = new Regex(@"^-?/d+/.?/d*$");
            return r.IsMatch(str);
        }
        #endregion

        #region 是否是数的字符
        /// <summary>
        /// 是否是数的字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumberValid(string str)
        {
            Regex r = new Regex(@"^/d+$");
            return r.IsMatch(str);
        }
        #endregion
        /// <summary>
        /// 获取远程页面代码..
        /// </summary>
        /// <param name="urlPath"></param>
        /// <returns></returns>
        public static string WebRequestGetString(string urlPath)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(urlPath);

            request.CookieContainer = new CookieContainer();
            request.CookieContainer.SetCookies(new Uri(urlPath), "");
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("GB2312"));
            string responseFromStream = reader.ReadToEnd();
            reader.Close();
            response.Close();
            return responseFromStream;
        }
        /// <summary>
        /// 弹出页面
        /// </summary>
        /// <param name="url"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static string JsOpenWindow(string url, int width, int height)
        {
            return "window.open(/"" + RemoveEnter(url) + "/",/"/",/"top=150,left=250,height=" + height.ToString() + ",width=" + width.ToString() + ",status=yes,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes/")";
        }

        private static string RemoveEnter(string input)
        {
            return input.Replace("/r", string.Empty);
        }
        /// <summary>
        /// 拆分字符串

        /// </summary>
        /// <param name="instrString">要拆分的字符串</param>
        /// <param name="instrSeparating">拆分的分隔串</param>
        /// <returns>拆分出的字符串数组</returns>
        public static string[] SplitString(string instrString, string instrSeparating)
        {
            return instrString.Split(instrSeparating.ToCharArray());
        }

        /// <summary>
        /// 用分号拆分字符串
        /// </summary>
        /// <param name="instrString">要拆分的字符串</param>
        /// <returns>拆分出的字符串数组</returns>
        public static string[] SplitString(string instrString)
        {
            return SplitString(instrString, ";");
        }
        /// <summary>
        /// 判断字符串是否为空

        /// </summary>
        /// <param name="instrBlank">要判断的字符串</param>
        /// <returns>空字符串:true 非空字符串:false</returns>
        public static bool IsBlank(string instrBlank)
        {
            if (instrBlank == null || instrBlank.Trim().Length == 0)
            {
                //有三种情况这里返回true
                //1.字符串为null
                //2.空字符串
                //3.只包含空格的字符串

                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 将信用卡号进行加密,只保留前后四位
        /// </summary>
        /// <param name="sCardNO">原有卡号</param>
        /// <returns>加密后的卡号</returns>
        public static string HiddenCardNO(string sCardNO)
        {
            if (sCardNO.Length >= 8)
            {
                sCardNO = sCardNO.Substring(0, 4) + "********" + sCardNO.Substring(sCardNO.Length - 4, 4);
            }
            return sCardNO;
        }
        /// <summary>
        /// 判断字符串中是否全是GB2312字符
        /// </summary>
        /// <param name="strText">字符串</param>
        /// <returns>false:含有有非GB2312字符</returns>
        public static bool IsGB2312code(string strText)
        {
            string sTemp = "";
            bool result = true;
            int asc = -1;
            for (int i = 0; i < strText.Length; i++)
            {
                sTemp = strText[i].ToString();
                asc = (int)strText[i];
                if (asc > 127 || asc < 0)//是汉字
                {
                    byte[] charbyte = Encoding.Default.GetBytes(strText[i].ToString().ToCharArray());
                    if ((charbyte[1] >= 64 && charbyte[1] <= 160) || (charbyte[0] >= 129 && charbyte[0] <= 160))
                    //     if((charbyte[1] >= 64 && charbyte[1] <= 160) || (charbyte[0] >= 129 && charbyte[0] < 176) || (charbyte[0] > 247) ) //update by 邢文龙2007-05-16
                    //     if((charbyte[1] > 254) || (charbyte[1] < 161) || (charbyte[0] < 176) || (charbyte[0] > 247)) //update by 邢文龙2007-05-17
                    {
                        result = false;
                        break;
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 从html中获取页面上的图片URL
        /// </summary>
        /// <param name="HtmlText"></param>
        /// <returns></returns>
        public static ArrayList GetImgSrcsFromHtml(string HtmlText)
        {
            #region 获取页面上的图片URL
            ArrayList Value = new ArrayList();
            Regex resrc = new Regex(@"/<img(.*)>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            if (resrc.Matches(HtmlText).Count > 0)
            {
                string src = resrc.Matches(HtmlText)[0].Value;
                Regex re = new Regex(@"<img[^>]*?src/s*=/s*(""(?<src>[^""]+?)""|'(?<src>[^']+?)'|(?<src>[^/s>]+))[^>]*?>", RegexOptions.IgnoreCase | RegexOptions.Singleline);

                foreach (Match srcMatch in re.Matches(src))
                {
                    string SrcUrl = srcMatch.Groups["src"].Value;
                    if (!Value.Contains(SrcUrl))
                    {
                        Value.Add(SrcUrl);
                    }
                }
            }
            return Value;
            #endregion
        }
        /// 从HTML中获取纯文本
        /// </summary>
        /// <param name="HtmlText">HTML</param>
        /// <returns>不含html标识的文本</returns>
        public static string GetPlainText(string HtmlText)
        {
            string Value = HtmlText.ToLower();
            Value = Regex.Replace(Value, "<[^>]*>", "", RegexOptions.Multiline | RegexOptions.IgnoreCase);
            return Value.Replace("/t", "").Replace("/r", "").Replace("/n", "").Replace(" ", "").Replace(" ", "").Replace("&nbsp;", "").Replace("&gt;", "").Trim();
        }

        #region 拼音变量
        private static int[] pyvalue = new int[]{-20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036,-20032,-20026,
               -20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763,-19756,-19751,-19746,-19741,-19739,-19728,
               -19725,-19715,-19540,-19531,-19525,-19515,-19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,
               -19261,-19249,-19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006,-19003,-18996,
               -18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735,-18731,-18722,-18710,-18697,-18696,-18526,
               -18518,-18501,-18490,-18478,-18463,-18448,-18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183,
               -18181,-18012,-17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752,-17733,-17730,
               -17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482,-17468,-17454,-17433,-17427,-17417,-17202,
               -17185,-16983,-16970,-16942,-16915,-16733,-16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,
               -16452,-16448,-16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216,-16212,-16205,
               -16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944,-15933,-15920,-15915,-15903,-15889,-15878,
               -15707,-15701,-15681,-15667,-15661,-15659,-15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,
               -15408,-15394,-15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150,-15149,-15144,
               -15143,-15141,-15140,-15139,-15128,-15121,-15119,-15117,-15110,-15109,-14941,-14937,-14933,-14930,-14929,-14928,
               -14926,-14922,-14921,-14914,-14908,-14902,-14894,-14889,-14882,-14873,-14871,-14857,-14678,-14674,-14670,-14668,
               -14663,-14654,-14645,-14630,-14594,-14429,-14407,-14399,-14384,-14379,-14368,-14355,-14353,-14345,-14170,-14159,
               -14151,-14149,-14145,-14140,-14137,-14135,-14125,-14123,-14122,-14112,-14109,-14099,-14097,-14094,-14092,-14090,
               -14087,-14083,-13917,-13914,-13910,-13907,-13906,-13905,-13896,-13894,-13878,-13870,-13859,-13847,-13831,-13658,
               -13611,-13601,-13406,-13404,-13400,-13398,-13395,-13391,-13387,-13383,-13367,-13359,-13356,-13343,-13340,-13329,
               -13326,-13318,-13147,-13138,-13120,-13107,-13096,-13095,-13091,-13076,-13068,-13063,-13060,-12888,-12875,-12871,
               -12860,-12858,-12852,-12849,-12838,-12831,-12829,-12812,-12802,-12607,-12597,-12594,-12585,-12556,-12359,-12346,
               -12320,-12300,-12120,-12099,-12089,-12074,-12067,-12058,-12039,-11867,-11861,-11847,-11831,-11798,-11781,-11604,
               -11589,-11536,-11358,-11340,-11339,-11324,-11303,-11097,-11077,-11067,-11055,-11052,-11045,-11041,-11038,-11024,
               -11020,-11019,-11018,-11014,-10838,-10832,-10815,-10800,-10790,-10780,-10764,-10587,-10544,-10533,-10519,-10331,
               -10329,-10328,-10322,-10315,-10309,-10307,-10296,-10281,-10274,-10270,-10262,-10260,-10256,-10254};
        private static string[] pystr = new string[]{"a","ai","an","ang","ao","ba","bai","ban","bang","bao","bei","ben","beng","bi","bian","biao",
               "bie","bin","bing","bo","bu","ca","cai","can","cang","cao","ce","ceng","cha","chai","chan","chang","chao","che","chen",
               "cheng","chi","chong","chou","chu","chuai","chuan","chuang","chui","chun","chuo","ci","cong","cou","cu","cuan","cui",
               "cun","cuo","da","dai","dan","dang","dao","de","deng","di","dian","diao","die","ding","diu","dong","dou","du","duan",
               "dui","dun","duo","e","en","er","fa","fan","fang","fei","fen","feng","fo","fou","fu","ga","gai","gan","gang","gao",
               "ge","gei","gen","geng","gong","gou","gu","gua","guai","guan","guang","gui","gun","guo","ha","hai","han","hang",
               "hao","he","hei","hen","heng","hong","hou","hu","hua","huai","huan","huang","hui","hun","huo","ji","jia","jian",
               "jiang","jiao","jie","jin","jing","jiong","jiu","ju","juan","jue","jun","ka","kai","kan","kang","kao","ke","ken",
               "keng","kong","kou","ku","kua","kuai","kuan","kuang","kui","kun","kuo","la","lai","lan","lang","lao","le","lei",
               "leng","li","lia","lian","liang","liao","lie","lin","ling","liu","long","lou","lu","lv","luan","lue","lun","luo",
               "ma","mai","man","mang","mao","me","mei","men","meng","mi","mian","miao","mie","min","ming","miu","mo","mou","mu",
               "na","nai","nan","nang","nao","ne","nei","nen","neng","ni","nian","niang","niao","nie","nin","ning","niu","nong",
               "nu","nv","nuan","nue","nuo","o","ou","pa","pai","pan","pang","pao","pei","pen","peng","pi","pian","piao","pie",
               "pin","ping","po","pu","qi","qia","qian","qiang","qiao","qie","qin","qing","qiong","qiu","qu","quan","que","qun",
               "ran","rang","rao","re","ren","reng","ri","rong","rou","ru","ruan","rui","run","ruo","sa","sai","san","sang",
               "sao","se","sen","seng","sha","shai","shan","shang","shao","she","shen","sheng","shi","shou","shu","shua",
               "shuai","shuan","shuang","shui","shun","shuo","si","song","sou","su","suan","sui","sun","suo","ta","tai",
               "tan","tang","tao","te","teng","ti","tian","tiao","tie","ting","tong","tou","tu","tuan","tui","tun","tuo",
               "wa","wai","wan","wang","wei","wen","weng","wo","wu","xi","xia","xian","xiang","xiao","xie","xin","xing",
               "xiong","xiu","xu","xuan","xue","xun","ya","yan","yang","yao","ye","yi","yin","ying","yo","yong","you",
               "yu","yuan","yue","yun","za","zai","zan","zang","zao","ze","zei","zen","zeng","zha","zhai","zhan","zhang",
               "zhao","zhe","zhen","zheng","zhi","zhong","zhou","zhu","zhua","zhuai","zhuan","zhuang","zhui","zhun","zhuo",
               "zi","zong","zou","zu","zuan","zui","zun","zuo"};
        #endregion

        /// <summary>
        /// 汉字转换全拼
        /// </summary>
        /// <param name="strChinese">要转换拼音的汉字</param>
        /// <returns>转换后的全拼</returns>
        public static string GetAllWordSpell(string strChinese)
        {
            #region 汉字转换全拼
            byte[] array = new byte[2];
            string returnstr = "";
            int chrasc = 0;
            int i1 = 0;
            int i2 = 0;
            char[] nowchar = strChinese.ToCharArray();
            for (int j = 0; j < nowchar.Length; j++)
            {
                array = System.Text.Encoding.Default.GetBytes(nowchar[j].ToString());
                i1 = (short)(array[0]);
                i2 = (short)(array[1]);
                chrasc = i1 * 256 + i2 - 65536;
                if (chrasc > 0 && chrasc < 160)
                {
                    returnstr += nowchar[j];
                }
                else
                {
                    for (int i = (pyvalue.Length - 1); i >= 0; i--)
                    {
                        if (pyvalue[i] < chrasc)
                        {
                            returnstr += pystr[i];
                            break;
                        }
                    }
                }
            }
            return returnstr;
            #endregion
        }

        /// <summary>
        /// 获得大写中文简拼
        /// </summary>
        /// <param name="strChinese">输入中文字符串</param>
        /// <returns>返回大写简拼</returns>
        public static string GetWordSpell(string strChinese)
        {
            #region 获得大写中文简拼
            int slen, xx;
            string Ss1;

            Ss1 = "";
            slen = strChinese.Length;
            if (slen == 0)
            {
                Ss1 = "";
                return Ss1;
            }

            Encoding strANSI = Encoding.Default;
            char[] c = new char[1];
            for (xx = 0; xx < slen; xx++)
            {
                c[0] = strChinese[xx];
                byte[] Hz = strANSI.GetBytes(c);

                if (Hz.GetLength(0) == 1)   // 普通字符
                {
                    Ss1 = Ss1 + c[0];
                    continue;
                }

                int i = Hz[0] * 256 + Hz[1];
                if (i >= 45217 && i < 45253)
                {
                    Ss1 = Ss1 + "A";
                }
                if (i >= 45253 && i < 45761)
                {
                    Ss1 = Ss1 + "B";
                }
                if (i >= 45761 && i < 46318)
                {
                    Ss1 = Ss1 + "C";
                }
                if (i >= 46318 && i < 46826)
                {
                    Ss1 = Ss1 + "D";
                }
                if (i >= 46826 && i < 47010)
                {
                    Ss1 = Ss1 + "E";
                }
                if (i >= 47010 && i < 47297)
                {
                    Ss1 = Ss1 + "F";
                }
                if (i >= 47297 && i < 47614)
                {
                    Ss1 = Ss1 + "G";
                }
                if (i >= 47614 && i < 48119)
                {
                    Ss1 = Ss1 + "H";
                }
                if (i >= 48119 && i < 49062)
                {
                    Ss1 = Ss1 + "J";
                }
                if (i >= 49062 && i < 49324)
                {
                    Ss1 = Ss1 + "K";
                }
                if (i >= 49324 && i < 49896)
                {
                    Ss1 = Ss1 + "L";
                }
                if (i >= 49896 && i < 50371)
                {
                    Ss1 = Ss1 + "M";
                }
                if (i >= 50371 && i < 50614)
                {
                    Ss1 = Ss1 + "N";
                }
                if (i >= 50614 && i < 50622)
                {
                    Ss1 = Ss1 + "O";
                }
                if (i >= 50622 && i < 50906)
                {
                    Ss1 = Ss1 + "P";
                }
                if (i >= 50906 && i < 51387)
                {
                    Ss1 = Ss1 + "Q";
                }
                if (i >= 51387 && i < 51446)
                {
                    Ss1 = Ss1 + "R";
                }
                if (i >= 51446 && i < 52218)
                {
                    Ss1 = Ss1 + "S";
                }
                if (i >= 52218 && i < 52698)
                {
                    Ss1 = Ss1 + "T";
                }
                if (i >= 52698 && i < 52980)
                {
                    Ss1 = Ss1 + "W";
                }
                if (i >= 52980 && i < 53689)
                {
                    Ss1 = Ss1 + "X";
                }
                if (i >= 53689 && i < 54481)
                {
                    Ss1 = Ss1 + "Y";
                }
                if (i >= 54481 && i < 55290)
                {
                    Ss1 = Ss1 + "Z";
                }
            }
            return Ss1;
            #endregion
        }

        /// <summary>
        /// 除去字符多余的分隔符,比如:";;酒店;;;;普通;;;"将返回"酒店;普通"
        /// </summary>
        /// <param name="str">待处理的字符串</param>
        /// <param name="separator">分隔符</param>
        /// <returns></returns>
        public static string TrimSeparator(string str, string separator)
        {
            #region  除去字符多余的分隔符,比如:";;酒店;;;;普通;;;"将返回"酒店;普通"
            try
            {
                if (null == str) return "";
                if (str.Equals("")) return str;

                char[] chr = separator.ToCharArray();
                string[] strlist = str.Split(chr);
                string newstr = "";
                for (int i = 0; i < strlist.Length; i++)
                {
                    if (strlist[i] == null || strlist[i].Trim().Equals("")) continue;
                    newstr += strlist[i].Trim() + separator;
                }
                newstr = newstr.TrimEnd(chr);
                return newstr;
            }
            catch
            {
                return str;
            }
            #endregion
        }
        /// <summary>
        /// 需用于处理的值,如传入DataRow1["colName"],true时将返回DataRow1["colName"].ToString()或者null值
        /// </summary>
        /// <param name="val">一个承载了值得对象</param>
        /// <param name="nullable">是否允许返回值为null</param>
        /// <returns></returns>
        public static string KillNull(object val, bool nullable)
        {
            if ((null == val) || (val == System.DBNull.Value))
            {
                return nullable ? null : "";
            }
            return val.ToString();
        }
        public static string KillNull(object val)
        {
            string str = "";
            if ((null == val) || (val == System.DBNull.Value) || val.ToString().Equals(""))
            {
                str = "无";
            }
            else
            {
                str = val.ToString();
            }
            return str;

        }
        /// <summary>
        /// 判断输入的字符是否为空,是的话,返回&nbsp;
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static string ToHTMLSpace(string Str)
        {
            if (Str.Length == 0)
            {
                return "&nbsp;";
            }
            else
            {
                return Str;
            }
        }

        /// <summary>
        /// 截取字符串的前几位
        /// </summary>
        /// <param name="Str">字符串</param>
        /// <param name="x">截取位数</param>
        /// <returns></returns>
        public static string GetStringPlace(string Str, int x)
        {
            if (Str.Trim().Length > x)
            {
                return Str.Substring(0, x).ToString();
            }
            else
            {
                return Str.Trim();
            }
        }
        /// <summary>
        /// 截取字符串的前几位,并在后面加点
        /// </summary>
        /// <param name="Str">字符串</param>
        /// <param name="x">截取位数</param>
        /// <param name="DotNum"></param>
        /// <returns></returns>
        public static string GetStringPlace(string Str, int x, int DotNum)
        {
            if (Str.Trim().Length > x)
            {
                return Str.Substring(0, x).ToString() + "…";
            }
            else
            {
                return Str.Trim();
            }
        }

        /// <summary>
        /// 检查输入的字符串是不是数字
        /// </summary>
        /// <param name="Str">数字形式的字符串</param>
        /// <returns>字符串是否为数字形式</returns>
        public static bool IsNum(string Str)
        {
            bool Rtn = true;
            try
            {
                double d1 = double.Parse(Str.Trim());
                return Rtn;
            }
            catch
            {
                Rtn = false;
                return Rtn;
            }
        }

        /// <summary>
        /// 判断输入字符串是不是日期时间形式
        /// </summary>
        /// <param name="Str">日期时间形式的字符串</param>
        /// <returns>输入字符串是不是日期时间形式:是或否</returns>
        public static bool IsDate(string Str)
        {
            try
            {
                DateTime dt = DateTime.Parse(Str.Trim());
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 把银行账号变成四位一空格形式
        /// </summary>
        /// <param name="AccountNumber">银行账号字符串</param>
        /// <returns>将传入字符串每四位增加一个空格,不足四位的返回原串</returns>
        public static string FormatAccountNumber(string AccountNumber)
        {
            string an;
            an = AccountNumber.Trim();
            if (an.Length <= 4)
            {
                return an;
            }
            else
            {
                string s = "";
                for (int i = 0; i < an.Length; i++)
                {

                    if ((i % 4) == 0)
                    {
                        s += " ";
                    }
                    s += an.Substring(i, 1);
                }
                return s;
            }
        }

        /// <summary>
        /// 把字符串中的”/r/n” 转换为”< br>”
        /// </summary>
        /// <param name="Str">含有”/r/n”的字符串</param>
        /// <returns>含有”< br>”的字符串</returns>
        public static string ConvertReturn(string Str)
        {
            string rtn = "";
            if (Str.Length > 0)
            {
                rtn = Str;
                rtn = rtn.Replace("/r/n", "<br>");
            }
            else
            {
                rtn = String.Empty;
            }

            return rtn;
        }

        /// <summary>
        /// 去掉字符串中的不需要的字符,字符在web.Config中定义。
        /// </summary>
        /// <param name="Str">字符串</param>
        /// <returns>去掉Mask后的字符串</returns>
        public static string RemoveChar(string Str)
        {
            string Mask = "<b>,</b>,<p>,</p>,<strong>,</strong>,*,&&,#,(周末特价),(周末特价),&";
            string OKStr = Str.ToLower();
            string delimStr = ",";
            char[] delimiter = delimStr.ToCharArray();
            string[] Arr = Mask.Split(delimiter);
            foreach (string s in Arr)
            {
                OKStr = OKStr.Replace(s, "");
            }
            return OKStr;
        }

        /// <summary>
        /// 去掉字符串中的”/r/n”
        /// </summary>
        /// <param name="Str">字符串</param>
        /// <returns>去掉”/r/n”后的字符串</returns>
        public static string RemoveReturn(string Str)
        {
            string rtn = "";
            if (Str.Length > 0)
            {
                rtn = Str;
                rtn = rtn.Replace("/r/n", "");
            }
            else
            {
                rtn = String.Empty;
            }

            return rtn;
        }

        /// <summary>
        /// 替换掉对数据库不安全的字符串
        /// </summary>
        /// <param name="Str">含有英文逗号等不安全字符的字符串</param>
        /// <returns>替换掉对数据库不安全的字符后的字符串</returns>
        public static string ParseTxt(string Str)
        {
            string rtn = "";

            if (Str.Length > 0)
            {
                rtn = Str.Trim();
                rtn = Str.Replace("'", "’");//把英文单引号替换成中文的,防止数据库写入错误
                //    rtn=rtn.Replace("<","(");//防止页面错误
                //    rtn=rtn.Replace(">",")");
            }
            else
            {
                rtn = string.Empty;
            }
            return rtn;
        }

        /// <summary>
        /// 得到字符串的字节长度
        /// </summary>
        /// <param name="Str">字符串</param>
        /// <returns>字节长度</returns>
        public static int GetByteLength(string Str)
        {
            byte[] bytes = Encoding.Default.GetBytes(Str);
            return bytes.Length;
        }

        public static string ShowStr(int TxtID, int LangID)
        {
            //HttpServerUtility S=new ;   
            string LangTxtFile = "";// HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["SiteRoot"].ToString() + "XML/Lang.xml");
            System.Xml.XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(LangTxtFile);
            XmlNodeList xnl = xmlDoc.DocumentElement.ChildNodes;
            string BackTxt = "";
            foreach (XmlNode node in xnl)
            {
                if (node.Attributes["ID"].Value == TxtID.ToString())
                {
                    if (LangID == 0)
                        BackTxt = node.Attributes["ENG"].Value;
                    else if (LangID == 142)
                        BackTxt = node.Attributes["CHN"].Value;
                    break;
                }
            }
            return BackTxt;
        }

        /// <summary>
        /// 将数字转化为相应的英文,仅限于1到9/Jove/06-07-06
        /// </summary>
        /// <param name="dig"></param>
        /// <returns></returns>
        public static string convertDigitToEng(int dig)
        {
            string strResult = "";

            switch (dig)
            {
                case 1:
                    strResult = "One";
                    break;

                case 2:
                    strResult = "Two";
                    break;

                case 3:
                    strResult = "Three";
                    break;

                case 4:
                    strResult = "For";
                    break;

                case 5:
                    strResult = "Five";
                    break;

                case 6:
                    strResult = "Six";
                    break;

                case 7:
                    strResult = "Seven";
                    break;

                case 8:
                    strResult = "Eight";
                    break;

                case 9:
                    strResult = "Nine";
                    break;
            }

            return strResult;
        }

        /// <summary>
        /// 汉字转换成拼音(全拼)
        /// </summary>
        /// <param name="pChinese"></param>
        /// <returns></returns>
        public static string ChineseToPinyin(string pChinese)
        {
            int[] pyvalue = new int[]{-20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036,-20032,-20026,
      -20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763,-19756,-19751,-19746,-19741,-19739,-19728,
      -19725,-19715,-19540,-19531,-19525,-19515,-19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,
      -19261,-19249,-19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006,-19003,-18996,
      -18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735,-18731,-18722,-18710,-18697,-18696,-18526,
      -18518,-18501,-18490,-18478,-18463,-18448,-18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183,
      -18181,-18012,-17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752,-17733,-17730,
      -17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482,-17468,-17454,-17433,-17427,-17417,-17202,
      -17185,-16983,-16970,-16942,-16915,-16733,-16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,
      -16452,-16448,-16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216,-16212,-16205,
      -16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944,-15933,-15920,-15915,-15903,-15889,-15878,
      -15707,-15701,-15681,-15667,-15661,-15659,-15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,
      -15408,-15394,-15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150,-15149,-15144,
      -15143,-15141,-15140,-15139,-15128,-15121,-15119,-15117,-15110,-15109,-14941,-14937,-14933,-14930,-14929,-14928,
      -14926,-14922,-14921,-14914,-14908,-14902,-14894,-14889,-14882,-14873,-14871,-14857,-14678,-14674,-14670,-14668,
      -14663,-14654,-14645,-14630,-14594,-14429,-14407,-14399,-14384,-14379,-14368,-14355,-14353,-14345,-14170,-14159,
      -14151,-14149,-14145,-14140,-14137,-14135,-14125,-14123,-14122,-14112,-14109,-14099,-14097,-14094,-14092,-14090,
      -14087,-14083,-13917,-13914,-13910,-13907,-13906,-13905,-13896,-13894,-13878,-13870,-13859,-13847,-13831,-13658,
      -13611,-13601,-13406,-13404,-13400,-13398,-13395,-13391,-13387,-13383,-13367,-13359,-13356,-13343,-13340,-13329,
      -13326,-13318,-13147,-13138,-13120,-13107,-13096,-13095,-13091,-13076,-13068,-13063,-13060,-12888,-12875,-12871,
      -12860,-12858,-12852,-12849,-12838,-12831,-12829,-12812,-12802,-12607,-12597,-12594,-12585,-12556,-12359,-12346,
      -12320,-12300,-12120,-12099,-12089,-12074,-12067,-12058,-12039,-11867,-11861,-11847,-11831,-11798,-11781,-11604,
      -11589,-11536,-11358,-11340,-11339,-11324,-11303,-11097,-11077,-11067,-11055,-11052,-11045,-11041,-11038,-11024,
      -11020,-11019,-11018,-11014,-10838,-10832,-10815,-10800,-10790,-10780,-10764,-10587,-10544,-10533,-10519,-10331,
      -10329,-10328,-10322,-10315,-10309,-10307,-10296,-10281,-10274,-10270,-10262,-10260,-10256,-10254};

            string[] pystr = new string[]{"a","ai","an","ang","ao","ba","bai","ban","bang","bao","bei","ben","beng","bi","bian","biao",
      "bie","bin","bing","bo","bu","ca","cai","can","cang","cao","ce","ceng","cha","chai","chan","chang","chao","che","chen",
      "cheng","chi","chong","chou","chu","chuai","chuan","chuang","chui","chun","chuo","ci","cong","cou","cu","cuan","cui",
      "cun","cuo","da","dai","dan","dang","dao","de","deng","di","dian","diao","die","ding","diu","dong","dou","du","duan",
      "dui","dun","duo","e","en","er","fa","fan","fang","fei","fen","feng","fo","fou","fu","ga","gai","gan","gang","gao",
      "ge","gei","gen","geng","gong","gou","gu","gua","guai","guan","guang","gui","gun","guo","ha","hai","han","hang",
      "hao","he","hei","hen","heng","hong","hou","hu","hua","huai","huan","huang","hui","hun","huo","ji","jia","jian",
      "jiang","jiao","jie","jin","jing","jiong","jiu","ju","juan","jue","jun","ka","kai","kan","kang","kao","ke","ken",
      "keng","kong","kou","ku","kua","kuai","kuan","kuang","kui","kun","kuo","la","lai","lan","lang","lao","le","lei",
      "leng","li","lia","lian","liang","liao","lie","lin","ling","liu","long","lou","lu","lv","luan","lue","lun","luo",
      "ma","mai","man","mang","mao","me","mei","men","meng","mi","mian","miao","mie","min","ming","miu","mo","mou","mu",
      "na","nai","nan","nang","nao","ne","nei","nen","neng","ni","nian","niang","niao","nie","nin","ning","niu","nong",
      "nu","nv","nuan","nue","nuo","o","ou","pa","pai","pan","pang","pao","pei","pen","peng","pi","pian","piao","pie",
      "pin","ping","po","pu","qi","qia","qian","qiang","qiao","qie","qin","qing","qiong","qiu","qu","quan","que","qun",
      "ran","rang","rao","re","ren","reng","ri","rong","rou","ru","ruan","rui","run","ruo","sa","sai","san","sang",
      "sao","se","sen","seng","sha","shai","shan","shang","shao","she","shen","sheng","shi","shou","shu","shua",
      "shuai","shuan","shuang","shui","shun","shuo","si","song","sou","su","suan","sui","sun","suo","ta","tai",
      "tan","tang","tao","te","teng","ti","tian","tiao","tie","ting","tong","tou","tu","tuan","tui","tun","tuo",
      "wa","wai","wan","wang","wei","wen","weng","wo","wu","xi","xia","xian","xiang","xiao","xie","xin","xing",
      "xiong","xiu","xu","xuan","xue","xun","ya","yan","yang","yao","ye","yi","yin","ying","yo","yong","you",
      "yu","yuan","yue","yun","za","zai","zan","zang","zao","ze","zei","zen","zeng","zha","zhai","zhan","zhang",
      "zhao","zhe","zhen","zheng","zhi","zhong","zhou","zhu","zhua","zhuai","zhuan","zhuang","zhui","zhun","zhuo",
      "zi","zong","zou","zu","zuan","zui","zun","zuo"};

            byte[] array = new byte[2];
            string returnstr = "";
            int chrasc = 0;
            int i1 = 0;
            int i2 = 0;
            char[] nowchar = pChinese.ToCharArray();
            for (int j = 0; j < nowchar.GetLength(0); j++)
            {
                array = System.Text.Encoding.Default.GetBytes(nowchar[j].ToString());
                if (array.GetLength(0) != 2)
                {
                    returnstr += nowchar[j];
                }
                else
                {
                    i1 = (short)(array[0]);
                    i2 = (short)(array[1]);

                    chrasc = i1 * 256 + i2 - 65536;
                    if (chrasc > 0 && chrasc < 160)
                    {
                        returnstr += nowchar[j];
                    }
                    else
                    {
                        for (int i = (pyvalue.Length - 1); i >= 0; i--)
                        {
                            if (pyvalue[i] <= chrasc)
                            {
                                returnstr += pystr[i];
                                break;
                            }
                        }
                    }
                }
            }
            return returnstr;
        }
    }
}
 

 
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

自己写了很久的.net论坛源代码分享给大家

经过断断续续自己写的.net 在不具备成熟的条件下终于开放了, 大体功能上没有什么问题, (项目周期 6个月), 如果有喜欢的朋友,可以参看一下http;//www.jfstudio.net中的服务论...

div 滚动条

为什么说“山外有山人外有人”,以前在做滚动条的时候我一直用框架,今天看到一个用div做滚动条的,并且还很漂亮,所以才有此言啊。 SCROLLBAR-BASE-COLOR: #bbeeff; OVERF...

点击小图看大图!

"http://www.w3.org/TR/html4/loose.dtd">无标题文档   function changeimage(obj)   {   obj.src = &qu...

ASP.net下实现像QQ或迅雷一样的从下面浮上个提醒消息框

var oPopup = window.createPopup();var popTop=50;function popmsg(msgstr){var winstr="";winstr+=" ";wi...

组件 访问被拒绝 --“/”应用程序中的服务器错误。IIS重启不行,系统注销也不行

“/”应用程序中的服务器错误。--------------------------------------------------------------------------------配置错误 ...

实现的不让同一个用户登陆

string test = "username";private void Page_Load(object sender, System.EventArgs e){   if(Application...

jsp读文件

java.lang.String strFileName; //文件名java.io.File objFile; //文件对象java.io.FileReader objFileReader; //读...
  • houzy
  • houzy
  • 2005-01-24 16:11
  • 664
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)