收集整理之.NET常用篇

来源:互联网 发布:3.3.5wlk数据库 编辑:程序博客网 时间:2024/06/07 20:48
 一个项目中使用到的通用函数库(5) 汉字相关操作

#region 汉字相关操作
        ///   <summary>  
        ///   获取一串汉字的拼音声母  
        ///   </summary>  
        ///   <param   name="chinese">Unicode格式的汉字字符串</param>  
        ///   <returns>拼音声母字符串</returns>  
        ///   <example>  
        ///   “新桥软件”转换为“xqrj”  
        ///   </example>  
        public static String ConvertToChn(String chinese)
        {
            char[] buffer = new char[chinese.Length];
            for (int i = 0; i < chinese.Length; i++)
            {
                buffer[i] = ConvertCHN(chinese[i]);
            }
            return new String(buffer);
        }

        ///   <summary>  
        ///   获取一个汉字的拼音声母  
        ///   </summary>  
        ///   <param   name="chinese">Unicode格式的一个汉字</param>  
        ///   <returns>汉字的声母</returns>  
        public static char ConvertCHN(Char chinese)
        {
            Encoding gb2312 = Encoding.GetEncoding("GB2312");
            Encoding unicode = Encoding.Unicode;

            //   Convert   the   string   into   a   byte[].  
            byte[] unicodeBytes = unicode.GetBytes(new Char[] { chinese });
            //   Perform   the   conversion   from   one   encoding   to   the   other.  
            byte[] asciiBytes = Encoding.Convert(unicode, gb2312, unicodeBytes);

            //   计算该汉字的GB-2312编码  
            int n = (int)asciiBytes[0] << 8;
            n += (int)asciiBytes[1];

            //   根据汉字区域码获取拼音声母  
            if (In(0xB0A1, 0xB0C4, n)) return 'a';
            if (In(0XB0C5, 0XB2C0, n)) return 'b';
            if (In(0xB2C1, 0xB4ED, n)) return 'c';
            if (In(0xB4EE, 0xB6E9, n)) return 'd';
            if (In(0xB6EA, 0xB7A1, n)) return 'e';
            if (In(0xB7A2, 0xB8c0, n)) return 'f';
            if (In(0xB8C1, 0xB9FD, n)) return 'g';
            if (In(0xB9FE, 0xBBF6, n)) return 'h';
            if (In(0xBBF7, 0xBFA5, n)) return 'j';
            if (In(0xBFA6, 0xC0AB, n)) return 'k';
            if (In(0xC0AC, 0xC2E7, n)) return 'l';
            if (In(0xC2E8, 0xC4C2, n)) return 'm';
            if (In(0xC4C3, 0xC5B5, n)) return 'n';
            if (In(0xC5B6, 0xC5BD, n)) return 'o';
            if (In(0xC5BE, 0xC6D9, n)) return 'p';
            if (In(0xC6DA, 0xC8BA, n)) return 'q';
            if (In(0xC8BB, 0xC8F5, n)) return 'r';
            if (In(0xC8F6, 0xCBF0, n)) return 's';
            if (In(0xCBFA, 0xCDD9, n)) return 't';
            if (In(0xCDDA, 0xCEF3, n)) return 'w';
            if (In(0xCEF4, 0xD188, n)) return 'x';
            if (In(0xD1B9, 0xD4D0, n)) return 'y';
            if (In(0xD4D1, 0xD7F9, n)) return 'z';
            return '/0';
        }

        private static bool In(int Lp, int Hp, int Value)
        {
            return ((Value <= Hp) && (Value >= Lp));
        }

        //将汉字转成拼音字头的方法“中华人民共和国”-->"ZHRMGHG"    

        // 是采用对应的区位的方法,但有些汉字不在这个范围里,大家试一下  

        public string hz2py(string hz)     //获得汉字的区位码  
        {
            byte[] sarr = System.Text.Encoding.Default.GetBytes(hz);
            int len = sarr.Length;
            if (len > 1)
            {
                byte[] array = new byte[2];
                array = System.Text.Encoding.Default.GetBytes(hz);

                int i1 = (short)(array[0] - '/0');
                int i2 = (short)(array[1] - '/0');

                //unicode解码方式下的汉字码  
                //                         array   =   System.Text.Encoding.Unicode.GetBytes(hz);  
                //                         int   i1   =   (short)(array[0]   -   '/0');  
                //                         int   i2   =   (short)(array[1]   -   '/0');  
                //                         int   t1   =   Convert.ToInt32(i1,16);  
                //                         int   t2   =   Convert.ToInt32(i2,16);  

                int tmp = i1 * 256 + i2;
                string getpychar = "*";//找不到拼音码的用*补位  

                if (tmp >= 45217 && tmp <= 45252) { getpychar = "A"; }
                else if (tmp >= 45253 && tmp <= 45760) { getpychar = "B"; }
                else if (tmp >= 47761 && tmp <= 46317) { getpychar = "C"; }
                else if (tmp >= 46318 && tmp <= 46825) { getpychar = "D"; }
                else if (tmp >= 46826 && tmp <= 47009) { getpychar = "E"; }
                else if (tmp >= 47010 && tmp <= 47296) { getpychar = "F"; }
                else if (tmp >= 47297 && tmp <= 47613) { getpychar = "G"; }
                else if (tmp >= 47614 && tmp <= 48118) { getpychar = "H"; }
                else if (tmp >= 48119 && tmp <= 49061) { getpychar = "J"; }
                else if (tmp >= 49062 && tmp <= 49323) { getpychar = "K"; }
                else if (tmp >= 49324 && tmp <= 49895) { getpychar = "L"; }
                else if (tmp >= 49896 && tmp <= 50370) { getpychar = "M"; }
                else if (tmp >= 50371 && tmp <= 50613) { getpychar = "N"; }
                else if (tmp >= 50614 && tmp <= 50621) { getpychar = "O"; }
                else if (tmp >= 50622 && tmp <= 50905) { getpychar = "P"; }
                else if (tmp >= 50906 && tmp <= 51386) { getpychar = "Q"; }
                else if (tmp >= 51387 && tmp <= 51445) { getpychar = "R"; }
                else if (tmp >= 51446 && tmp <= 52217) { getpychar = "S"; }
                else if (tmp >= 52218 && tmp <= 52697) { getpychar = "T"; }
                else if (tmp >= 52698 && tmp <= 52979) { getpychar = "W"; }
                else if (tmp >= 52980 && tmp <= 53640) { getpychar = "X"; }
                else if (tmp >= 53689 && tmp <= 54480) { getpychar = "Y"; }
                else if (tmp >= 54481 && tmp <= 55289) { getpychar = "Z"; }
                return getpychar;
            }
            else
            {
                return hz;
            }
        }

        public string transpy(string strhz)     //把汉字字符串转换成拼音码  
        {
            string strtemp = "";
            int strlen = strhz.Length;
            for (int i = 0; i <= strlen - 1; i++)
            {
                strtemp += hz2py(strhz.Substring(i, 1));
            }
            return strtemp;
        }  
        #endregion 

IP相关:

#region 客户端信息相关
        
#region 获取客户IP
        
public static string getUserIp()
        {
            
return HttpContext.Current.Request.UserHostAddress;
        }

        
/// <summary>
        
/// 猎取客户端IP地址
        
/// </summary>
        
/// <param name="page">调用该方法的页面</param>
        
/// <returns>返回的IP地址字符串</returns>
        public static string GetClientIP(System.Web.UI.Page page)
        {
            
string ipAddress = "";
            
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"== null)
            {
                ipAddress 
= HttpContext.Current.Request.ServerVariables["Remote_Addr"];
            }
            
else
            {
                ipAddress 
= HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            }
            
return ipAddress;
        }
        
#endregion

        
#region 获取客户电脑名
        
public static string getUserHostName()
        {
            
return HttpContext.Current.Server.MachineName.ToString();
        }
        
#endregion
        
#endregion
#region 客户端信息相关
        
#region 获取客户IP
        
public static string getUserIp()
        {
            
return HttpContext.Current.Request.UserHostAddress;
        }

        
/// <summary>
        
/// 猎取客户端IP地址
        
/// </summary>
        
/// <param name="page">调用该方法的页面</param>
        
/// <returns>返回的IP地址字符串</returns>
        public static string GetClientIP(System.Web.UI.Page page)
        {
            
string ipAddress = "";
            
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"== null)
            {
                ipAddress 
= HttpContext.Current.Request.ServerVariables["Remote_Addr"];
            }
            
else
            {
                ipAddress 
= HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            }
            
return ipAddress;
        }
        
#endregion

        
#region 获取客户电脑名
        
public static string getUserHostName()
        {
            
return HttpContext.Current.Server.MachineName.ToString();
        }
        
#endregion
        
#endregion
IO相关:
#region 文件IO操作!
        
/// <summary>
        
/// 创建/写入文件内容
        
/// </summary>
        
/// <param name="FileName">文件名(默认当前目录/包含路径)</param>
        
/// <param name="FileContent">文件内容</param>
        
/// <param name="act">改写(false)/追加到文件尾部(true)</param>
        
/// <returns>返回bool</returns>
        public static bool IO_CreatTextFile(string FileName, string FileContent, bool act)
        {
            
try
            {
                StreamWriter writer1 
= new StreamWriter(FileName, act, Encoding.Default);
                writer1.Write(FileContent);
                writer1.Close();
            }
            
catch
            {
                
return false;
            }
            
return true;
        }
        
/// <summary>
        
/// 得到文件内容
        
/// </summary>
        
/// <param name="TextFilePath">文件路径</param>
        
/// <returns>文件内容字符串</returns>
        public static string IO_GetFileContent(string TextFilePath)
        {
            FileStream stream1 
= new FileStream(TextFilePath, FileMode.Open, FileAccess.Read);
            
byte[] buffer1 = new byte[(int)stream1.Length];
            stream1.Read(buffer1, 
0, buffer1.Length);
            stream1.Close();
            
return Encoding.Default.GetString(buffer1);
        }
        
/// <summary>
        
/// 读取文件内容
        
/// </summary>
        
/// <param name="TextFilePath">文件路径</param>
        
/// <returns></returns>
        public static string IO_GetFileContent1(string TextFilePath)
        {
            FileStream fs 
= new FileStream(TextFilePath, FileMode.Open);
            StreamReader sr 
= new StreamReader(fs);
            
string s = "";
            
while (sr.BaseStream.Position < sr.BaseStream.Length)
            {
                s 
+= sr.ReadLine();
            }
            
return s;
        }


        
#endregion
字符操作:
#region  字符操作
        
/// <summary>
        
/// 文本调试模版
        
/// </summary>
        public const string Text_DebugTemplate = "<div align="center"><div style="Background-color:#f3f3f3;color:#000000;width:75%;height:120px;font-family:'Times New Roman';font-size:14px;border:1px #cccccc dotted;padding:5px;">" +
            
"<fieldset style="height:100%"><legend>===========调试信息==============</legend>" +
            
"<div align="left"  style="text-indent:24px;font-family:fixedsys;font-size:12px;color:red;word-break:break-all;line-height:150%;padding-left:32px;padding-right:32px;">[信息内容]</div>" +
            
"</fieldset></div></div>";

        
#region 编码和格式转化
        
/// <summary>
        
/// 从一种编码到另一种编码
        
/// </summary>
        
/// <param name="str">源字符串</param>
        
/// <param name="From">From</param>
        
/// <param name="To">To</param>
        
/// <returns></returns>
        string ConertStr(string str, string From, string To)
        {

            
byte[] bs = System.Text.Encoding.GetEncoding(From).GetBytes(str);
            bs 
= System.Text.Encoding.Convert(System.Text.Encoding.GetEncoding(From), System.Text.Encoding.GetEncoding(To), bs);
            
string res = System.Text.Encoding.GetEncoding(To).GetString(bs);
            
return res;

        }

        
/// <summary>
        
/// 获取能够在客户端显示的XML文本内容
        
/// </summary>
        
/// <param name="OStr">文本内容</param>
        
/// <returns>合格的XML文本内容</returns>
        public static string Text_GetXMLString(string OStr)
        {
            
return OStr.Replace("'""").Replace(">""&gt;").Replace("<""&lt;").Replace("&""&amp;");
        }

        
/// <summary>
        
/// 获取能够在客户端显示的Javascript文本内容
        
/// </summary>
        
/// <param name="OStr">文本内容</param>
        
/// <returns>合格的Javascript文本内容</returns>
        public static string Text_GetJsString(string OStr)
        {
            
return OStr.Replace("'""/'").Replace(""""/" + """).Replace(" ""/" + "n").Replace(" ""/" + "r").Replace(" ""/" + "n" + "/" + "r");
        }

        
/// <summary>
        
/// 普通文本转换为HTML文本
        
/// </summary>
        
/// <param name="sText">普通文本内容</param>
        
/// <returns>HTML格式文本</returns>
        public static string Text_Plain2Html(string sText)
        {
            
string rStr = string.Empty;
            
if (sText.Length > 0)
            {
                rStr 
= sText.Replace(" ""<br>").Replace(" ""<br>").Replace(" ""<br>").Replace(" ""&nbsp;");
            }
            
else
            {
                rStr 
= "";
            }
            
return rStr;

        }

        
/// <summary>
        
/// 数组转换为普通字符
        
/// </summary>
        
/// <param name="StringArray">字符数组</param>
        
/// <param name="Sperator">字符分隔符</param>
        
/// <returns>该数组的字符串表现形式</returns>
        public static string Text_ArrayToString(string[] StringArray, string Sperator)
        {
            
int Len = StringArray.Length;
            
string ReturnString = String.Empty;
            
for (int i = 0; i < Len; i++)
            {
                ReturnString 
+= (i > 0? (Sperator + StringArray[i]) : (StringArray[i]);
            }
            
return ReturnString;
        }


        
/// <summary>
        
/// 数组转换为普通字符
        
/// </summary>
        
/// <param name="IntArrayArray">整形数组</param>
        
/// <param name="Sperator">字符分隔符</param>
        
/// <returns>该数组的字符串表现形式</returns>
        public static string Text_ArrayToString(int[] IntArray, string Sperator)
        {
            
int Len = IntArray.Length;
            
string ReturnString = String.Empty;
            
for (int i = 0; i < Len; i++)
            {
                ReturnString 
+= (i > 0? (Sperator + IntArray[i].ToString()) : (IntArray[i].ToString());
            }
            
return ReturnString;
        }

        
/// <summary>
        
/// 返回字符的ASCII值
        
/// </summary>
        
/// <param name="String">字符</param>
        
/// <returns>该字符的ASCII</returns>
        public static int Text_Asc(char __String)
        {
            
int num1;
            
int num2;
            
byte[] array1;
            
char[] array2;
            Encoding encoding1;
            
int num3;
            
byte num4;
            
char[] array3;


            num2 
= Convert.ToInt32(__String);
            
if (num2 < 128)
            {
                
return num2;
            }
            
try
            {
                encoding1 
= Encoding.Default;//------当前代码页
                array3 = new char[1];
                array3[
0= __String;
                array2 
= array3;

                
if (encoding1.GetMaxByteCount(1== 1)
                {
                    array1 
= new byte[1];
                    num3 
= encoding1.GetBytes(array2, 01, array1, 0);
                    num1 
= array1[0];
                    
return num1;

                }
                array1 
= new byte[2];
                num3 
= encoding1.GetBytes(array2, 01, array1, 0);
                
if (num3 == 1)
                {
                    num1 
= array1[0];
                    
return num1;

                }
                
if (BitConverter.IsLittleEndian)
                {
                    num4 
= array1[0];
                    array1[
0= array1[1];
                    array1[
1= num4;

                }
                num1 
= BitConverter.ToInt16(array1, 0);

            }
            
catch (Exception exception1)
            {
                
throw exception1;

            }
            
return num1;
        }

        
/// <summary>
        
/// 去处int[]数组里面的重复项
        
/// </summary>
        
/// <param name="myData">用于处理的数组</param>
        
/// <returns>返回处理后 没有重复项的数组</returns>
        public static int[] RemoveDup(int[] myData)
        {
            
if (myData.Length > 0)
            {
                Array.Sort(myData);

                
int size = 1;  //at  least  1  
                for (int i = 1; i < myData.Length; i++)
                    
if (myData[i] != myData[i - 1])
                        size
++;

                
int[] myTempData = new int[size];

                
int j = 0;

                myTempData[j
++= myData[0];

                
for (int i = 1; i < myData.Length; i++)
                    
if (myData[i] != myData[i - 1])
                        myTempData[j
++= myData[i];

                
return myTempData;
            }

            
return myData;
        }
        
/// <summary>
        
/// 替换重复的数组元素
        
/// </summary>
        
/// <param name="ObjStr">字符串</param>
        
/// <param name="separator">字符串中的分隔符</param>
        
/// <returns>不含重复的数组元素</returns>
        public static string RWDistinct(string ObjStr, string separator)
        {
            
int i, s;
            
string sResult = "";
            
if (ObjStr == string.Empty) { sResult = ""; }
            
else
            {
                
string[] arrStr = System.Text.RegularExpressions.Regex.Split(ObjStr, separator);
                
for (i = 0; i < arrStr.Length; i++)
                {
                    
if (i == 0) { sResult = arrStr[0]; }
                    
else if (i == 1)
                    { sResult 
+= (arrStr[1== sResult) ? (string.Empty) : (separator + arrStr[1]); }
                    
else
                    {
                        
bool StrExist = false;
                        
string[] resultArray = System.Text.RegularExpressions.Regex.Split(sResult, separator);
                        
for (s = 0; s < resultArray.Length; s++)
                        {
                            
if (resultArray[s] == arrStr[i])
                            { StrExist 
= truebreak; }
                        }
                        sResult 
+= StrExist ? string.Empty : (separator + arrStr[i]);
                    }
                }
            }
            
return sResult;
        }

        
/// <summary>
        
/// 客户端输出调式信息
        
/// </summary>
        
/// <param name="Message">信息内容(字符)</param>
        public static void Text_DebugString(string Message)
        {
            HttpContext.Current.Response.Write(Text_DebugTemplate.Replace(
"[信息内容]", Message));
        }


        
/// <summary>
        
/// 客户端输出调式信息,自定义标题。
        
/// </summary>
        
/// <param name="Message">信息内容(字符)</param>
        
/// <param name="Topic">标题</param>
        public static void Text_DebugString(string Message, string Topic)
        {
            HttpContext.Current.Response.Write(Text_DebugTemplate.Replace(
"[信息内容]", Message).Replace("调试信息", Topic));
        }

        
/// <summary>
        
/// 客户端输出调式信息
        
/// </summary>
        
/// <param name="Message">信息内容(字符组)</param>
        public static void Text_DebugString(string[] Message)
        {
            StringBuilder sb 
= new StringBuilder();
            
for (int i = 0; i < Message.Length; i++)
            {
                sb.Append(Message[i] 
+ "<br><br>");
            }
            
string sMessage = sb.ToString();
            HttpContext.Current.Response.Write(Text_DebugTemplate.Replace(
"[信息内容]", sMessage));
        }
        
/// <summary>
        
/// 
        
/// </summary>
        
/// <param name="ObjStr"></param>
        
/// <param name="separator"></param>
        
/// <returns></returns>
        public static string Text_DistinctString(string ObjStr, string separator)
        {
            
string text1 = "";
            
if (ObjStr == string.Empty)
            {
                text1 
= "";
            }
            
else
            {
                
string[] textArray1 = Regex.Split(ObjStr, separator);
                
for (int num1 = 0; num1 < textArray1.Length; num1++)
                {
                    
if (num1 == 0)
                    {
                        text1 
= textArray1[0];
                        
continue;
                    }
                    
if (num1 == 1)
                    {
                        text1 
= text1 + ((textArray1[1== text1) ? string.Empty : (separator + textArray1[1]));
                        
continue;
                    }
                    
bool flag1 = false;
                    
string[] textArray2 = Regex.Split(text1, separator);
                    
for (int num2 = 0; num2 < textArray2.Length; num2++)
                    {
                        
if (textArray2[num2] == textArray1[num1])
                        {
                            flag1 
= true;
                            
break;
                        }
                    }
                    text1 
= text1 + (flag1 ? string.Empty : (separator + textArray1[num1]));
                }
            }
            
return text1;
        }

        
/// <summary>
        
/// 代码内容里的文件,主要处理[IMG]标记里的文件。
        
/// </summary>
        
/// <param name="UBBContent">UBB代码内容</param>
        
/// <returns>所有本地服务器文件的相对(根目录)路径</returns>
        public static string Text_GetUBBFiles(string UBBContent, string FileSperator)
        {
            
string str1 = String.Empty;
            UBBContent 
= UBBContent.ToLower();
            
if (UBBContent.IndexOf("[img]"!= -1 && UBBContent.IndexOf("[/img]"!= -1)
            {
                
int i = 0;
                
int j = 0;
                
int k = 0;
                
for (; UBBContent.IndexOf("[img]", i) != -1; i = j + 6)
                {
                    i 
= UBBContent.IndexOf("[img]", i) + 5;
                    j 
= UBBContent.IndexOf("[/img]", i);
                    
string str2 = UBBContent.Substring(i, j - i);
                    
if (str2.IndexOf("http"== -1)
                    {
                        str1 
= String.Concat(str1, (k != 0? String.Concat(FileSperator, str2) : str2);
                        k
++;
                    }
                }
            }
            
return str1;
        }

        
public static string Text_getByteString(string String, int ByteLen)
        {
            
byte[] buffer1 = Encoding.Default.GetBytes(String);
            
if (buffer1.Length > ByteLen)
            {
                
return Encoding.Default.GetString(buffer1, 0, ByteLen);
            }
            
return String;
        }

        
public static string Text_GetExt(string FileName)
        {
            
if ((FileName != string.Empty) && (FileName.IndexOf("."!= -1))
            {
                
return FileName.Substring(FileName.LastIndexOf("."+ 1);
            }
            
return string.Empty;
        }

        
public static string Text_GetSize(int filesize)
        {
            
int num1;
            
if (filesize <= 0)
            {
                
return "未知";
            }
            
if (filesize > 0x100000)
            {
                num1 
= filesize / 0x100000;
                
return (num1.ToString() + "M");
            }
            
if (filesize > 0x400)
            {
                num1 
= filesize / 0x400;
                
return (num1.ToString() + "K");
            }
            
return (filesize.ToString() + "B");
        }



        
public static bool Text_RubbishString(string PostString)
        {
            
return false;
        }
        
#endregion

        
#region 字符串长度/截取

        
/// <summary>
        
/// 检测含有中文字符串的实际长度
        
/// </summary>
        
/// <param name="str">字符串</param>
        public static int cnLenth(string str)
        {
            System.Text.ASCIIEncoding n 
= new System.Text.ASCIIEncoding();
            
byte[] b = n.GetBytes(str);
            
int l = 0// l 为字符串之实际长度
            for (int i = 0; i <= b.Length - 1; i++)
            {
                
if (b[i] == 63//判断是否为汉字或全脚符号
                {
                    l
++;
                }
                l
++;
            }
            
return l;
        }

        
//public static string SubString(string stringToSub, int length)
        
//{
        
//    Regex regex = new Regex([u4e00-u9fa5]+, RegexOptions.Compiled);
        
//    char[] stringChar = stringToSub.ToCharArray();
        
//    StringBuilder sb = new StringBuilder();
        
//    int nLength = 0;

        
//    for(int i = 0; i < stringChar.Length; i++) {
        
//        if (regex.IsMatch((stringChar[i]).ToString())) {
        
//            sb.Append(stringChar[i]);
        
//            nLength += 2;
        
//        }
        
//        else {
        
//            sb.Append(stringChar[i]);
        
//            nLength = nLength + 1;
        
//        }

        
//        if (nLength > length)
        
//            break;
        
//    }

        
//    return sb.ToString();
        
//

        
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), @"[一-龥]+"))
                {
                    j 
+= 2;
                }
                
else
                {
                    j 
+= 1;
                }
                
if (j <= length)
                {
                    k 
+= 1;
                }
                
if (j >= length)
                {
                    
return temp.Substring(0, k);
                }
            }
            
return temp;
        }

        
public static string getStr(string s, int l)
        {
            
string temp = s;
            
if (Regex.Replace(temp, "[一-龥]""zz", RegexOptions.IgnoreCase).Length <= l)
            {
                
return temp;
            }
            
for (int i = temp.Length; i >= 0; i--)
            {
                temp 
= temp.Substring(0, i);
                
if (Regex.Replace(temp, "[一-龥]""zz", RegexOptions.IgnoreCase).Length <= l - 3)
                {
                    
return temp + "";
                }
            }
            
return "";
        }


        
public static string leftCut(string str_value, int str_len)
        {
            
int p_num = 0;
            
int i;
            
string New_Str_value = "";

            
if (str_value == "")
            {
                New_Str_value 
= "";
            }
            
else
            {
                
int Len_Num = str_value.Length;

                
//if (Len_Num < str_len)
                
//{
                
// str_len = Len_Num;
                
//}


                
for (i = 0; i <= Len_Num - 1; i++)
                {
                    
//str_value.Substring(i,1);
                    if (i > Len_Num) break;
                    
char c = Convert.ToChar(str_value.Substring(i, 1));
                    
if (((int)c > 255|| ((int)c < 0))
                    {
                        p_num 
= p_num + 2;

                    }
                    
else
                    {
                        p_num 
= p_num + 1;

                    }

                    
if (p_num >= str_len)
                    {

                        New_Str_value 
= str_value.Substring(0, i + 1);

                        
break;
                    }
                    
else
                    {
                        New_Str_value 
= str_value;
                    }

                }

            }
            
return New_Str_value;
        }

        
/// <summary>
        
/// 字符长度控制 中文 英文识别!
        
/// 注:一个汉字作为2个字符长度处理
        
/// </summary>
        
/// <param name="str">要进行切割的字符串</param>
        
/// <param name="len">返回的长度(自动识别中英文)</param>
        
/// <returns></returns>
        public static string CutString(string str, int len)
        {
            
byte[] sarr = System.Text.Encoding.Default.GetBytes(str);

            
if (sarr.Length > len)
                
return System.Text.Encoding.Default.GetString(sarr, 0, len) + "...";
            
else
                
return str;
        }

        
/// <summary>
        
/// 字符长度控制 中文 英文识别!
        
/// 注:一个汉字作为2个字符长度处理
        
/// </summary>
        
/// <param name="str">要进行切割的字符串</param>
        
/// <param name="len">返回的长度(自动识别中英文)</param>
        
/// <param name="isExt">是否输出...</param>
        
/// <returns></returns>
        public static string CutString(string str, int len, bool isExt)
        {
            
byte[] sarr = System.Text.Encoding.Default.GetBytes(str);

            
if (sarr.Length > len)
                
return System.Text.Encoding.Default.GetString(sarr, 0, len) + (isExt ? "..." : "");
            
else
                
return str;
        }
        
#endregion

        
#endregion
客户信息:
#region  客户端模拟函数-(提示信息等)

        
#region ClientAlert
        
/// <summary>
        
/// 客户端脚本:alert(Msg) I;
        
/// </summary>
        
/// <param name="Msg">要显示的消息</param>
        
/// <returns>alert函数的客户端脚本</returns>
        public static string Client_Alert(string Msg)
        {
            
return "<script language="javascript">alert("" + Msg + "");</script>";
        }

        
///<summary>
        
/// 客户端脚本:alert(Msg) II;
        
///</summary>
        
///<param name="Msg">要显示的消息</param>
        
///<param name="returnURL">返回地址</param>
        
///<returns>返回指定的URL</returns>
        public static string Client_Alert(string Msg, string returnURL)
        {
            
return "<script language="javascript">alert("" + Msg + "");location.href='" + returnURL + "';</script>";
        }

        
/// <summary>
        
/// 客户端脚本:confrim(Msg) I;
        
/// </summary>
        
/// <param name="Msg">待确认的消息</param>
        
/// <param name="url">确认之后转向的地址</param>
        
/// <returns>confirm函数的客户端脚本</returns>
        public static string Client_Confirm(string Msg, string url)
        {
            
string scripts = "<script language="javascript">" +
                
"if (confirm('" + Msg + "')) " +
                
" { location.href='" + url + "'; }" +
                
"</script>";
            
return scripts;
        }

        
/// <summary>
        
/// 客户端脚本:confrim(Msg) II;
        
/// </summary>
        
/// <param name="Msg">待确认的消息</param>
        
/// <param name="cfmurl">确认之后转向的地址</param>
        
/// <param name="retrunURL">取消之后转向的地址</param>
        
/// <returns>confirm函数的客户端脚本</returns>
        public static string Client_Confirm(string Msg, string cfmurl, string retrunURL)
        {
            
string scripts = "<script language="javascript">" +
                
"if (confirm('" + Msg + "')) " +
                
" { location.href='" + cfmurl + "'; }" +
                
"else { location.href='" + retrunURL + "'; }</script>";
            
return scripts;
        }
        
/// <summary>
        
/// 关闭窗口无提示信息!
        
/// </summary>
        public static string Client_CloseWindow()
        {
            
return "<script>window.opener=null;window.close();</script>";
        }
        
/// <summary>
        
/// 关闭 窗口前出现提示信息
        
/// </summary>
        
/// <param name="MSG">提示关闭信息</param>
        public static string Client_CloseWindow(string MSG)
        {
            
return "<script>if(confirm('" + MSG + "')){window.opener=null;window.close();}else{return false;}</script>";
        }

        
/// <summary>
        
/// 客户端脚本:重定向网址
        
/// </summary>
        
/// <param name="URL">重定向的网址</param>
        
/// <param name="CopyHistory">是否记录历史</param>
        
/// <returns>重定向网址的客户端脚本块</returns>
        public static string Client_Redirect(string URL, bool CopyHistory)
        {
            
return (CopyHistory) ? "<script language="javascript">top.location.href='" + URL + "';</script>" : "<script language="javascript">top.location.replace('" + URL + "');</script>";
        }
        
#endregion

        
#region 提示信息Response版本
        
/// <summary>
        
/// window.alert的提示信息
        
/// </summary>
        
/// <param name="strBody">提示信息内容</param>
        
/// <param name="strLocalUrl">转向地址</param>
        
/// <returns></returns>
        public static void WriteAlert(string strBody, string strLocalUrl)
        {
            HttpContext.Current.Response.Write(
"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>MSG</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <link href="../css/css.css" type="text/css" rel="Stylesheet" /> ");
            HttpContext.Current.Response.Write(
"<script language="javascript" type="text/javascript" src="../js/validateForm.js"></script> ");
            HttpContext.Current.Response.Write(
"<script language=javascript>  function redir(){ location.href= '" + strLocalUrl + "';}  function tmRedir(){ window.setTimeout('redir()', 100);} </script> ");
            HttpContext.Current.Response.Write(
"</head> <body>  <script language="javascript">  function stop(){return false; }  document.oncontextmenu=stop; </script> <script language=javascript> _error_msg_show('" + strBody + "', 'tmRedir()', '', ''); </script></body></html>");
            HttpContext.Current.Response.End();

        }

        
public static void WriteDialogAlert(string strBody, string strLocalUrl)
        {
            HttpContext.Current.Response.Write(
"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>MSG</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <link href="../css/css.css" type="text/css" rel="Stylesheet" /> ");
            HttpContext.Current.Response.Write(
"<script language="javascript" type="text/javascript" src="../js/validateForm2.js"></script> ");
            HttpContext.Current.Response.Write(
"<script language=javascript>  function redir(){ location.href= '" + strLocalUrl + "';}  function tmRedir(){ window.setTimeout('redir()', 100);} </script> ");
            HttpContext.Current.Response.Write(
"</head> <body>  <script language="javascript">  function stop(){return false; }  document.oncontextmenu=stop; </script> <script language=javascript> _error_msg_show('" + strBody + "', 'tmRedir()', '', ''); </script></body></html>");
            HttpContext.Current.Response.End();

        }

        
/// <summary>
        
/// window.alert的提示信息
        
/// </summary>
        
/// <param name="strBody">提示信息</param>
        
/// <param name="strLocalUrl">转向地址</param>
        
/// <param name="blHoldRequest">是否保留当前页面的Request到转向地址页面</param>
        public static void WriteAlert(string strBody, string strLocalUrl, bool blHoldRequest)
        {
            HttpContext.Current.Response.Write(
"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>MSG</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <link href="../css/css.css" type="text/css" rel="Stylesheet" /> </head> <body> ");
            HttpContext.Current.Response.Write(
"<script language="javascript" type="text/javascript" src="../js/validateForm.js"></script> ");
            HttpContext.Current.Response.Write(
"<script language=javascript>  function redir(){ return false;} </script> ");
            HttpContext.Current.Response.Write(
"</body></html>");
            
//HttpContext.Current.Response.End();
            HttpContext.Current.Server.Transfer(strLocalUrl, blHoldRequest);
        }

        
/// <summary>
        
/// window.alert的提示信息
        
/// </summary>
        
/// <param name="strBody">提示信息内容</param>
        
/// <returns></returns>
        public static void WriteAlert(string strBody)
        {
            HttpContext.Current.Response.Write(
"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>MSG</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <link href="../css/css.css" type="text/css" rel="Stylesheet" /> ");
            HttpContext.Current.Response.Write(
"<script language="javascript" type="text/javascript" src="../js/validateForm.js"></script> ");
            HttpContext.Current.Response.Write(
"<script language=javascript>  function redir(){ return false;} </script> ");
            HttpContext.Current.Response.Write(
"</head> <body> <script language=javascript> _error_msg_show('" + strBody + "', 'redir()', '', ''); </script></body></html>");
            
//HttpContext.Current.Response.End();
        }

        
public static void WriteDialogAlert(string strBody)
        {
            HttpContext.Current.Response.Write(
"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>MSG</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <link href="../css/css.css" type="text/css" rel="Stylesheet" /> ");
            HttpContext.Current.Response.Write(
"<script language="javascript" type="text/javascript" src="../js/validateForm2.js"></script> ");
            HttpContext.Current.Response.Write(
"<script language=javascript>  function redir(){ return false;} </script> ");
            HttpContext.Current.Response.Write(
"</head> <body> <script language=javascript> _error_msg_show('" + strBody + "', 'redir()', '', ''); </script></body></html>");
            
//HttpContext.Current.Response.End();
        }

        
/// <summary>
        
/// window.alert的提示信息首页使用
        
/// </summary>
        
/// <param name="strBody">提示信息内容</param>
        
/// <returns></returns>
        public static void indexWriteAlert(string strBody)
        {
            HttpContext.Current.Response.Write(
"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>MSG</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <link href="../css/css.css" type="text/css" rel="Stylesheet" /> ");
            HttpContext.Current.Response.Write(
"<script language="javascript" type="text/javascript" src="js/dialog.js"></script> ");
            HttpContext.Current.Response.Write(
"<script language=javascript>  function redir(){ return false;} </script> ");
            HttpContext.Current.Response.Write(
"</head> <body> <script language=javascript> _error_msg_show('" + strBody + "', 'redir()', '', ''); </script></body></html>");
            
//HttpContext.Current.Response.End();
        }
        
#endregion

        
/// <summary>
        
/// 打开新窗口
        
/// </summary>
        
/// <param name="URL">新窗口的URL</param>
        
/// <param name="WindowName">新窗口显示的名称</param>
        
/// <param name="Toolbar">是否显示工具栏,yes为显示</param>
        
/// <param name="Menubar">是否显示菜单栏,yes为显示</param>
        
/// <param name="Scrollbars">是否显示滚动栏,yes为显示</param>
        
/// <param name="Resizable">是否允许改变窗口大小,yes为允许</param>
        
/// <param name="Location">是否显示地址栏,yes为允许</param>
        
/// <param name="Status">是否显示状态栏内的信息(通常是文件已经打开),yes为允许</param>
        
/// <param name="Height">窗口高度</param>
        
/// <param name="Width">窗口宽度</param>
        
/// <param name="Top">窗口距离屏幕上方的象素值</param>
        
/// <param name="Left">窗口距离屏幕左侧的象素值</param>
        public static string Client_Open(string URL, string WindowName, bool Toolbar, bool Menubar, bool Scrollbars, bool Resizable, bool Location, bool Status, int Height, int Width, int Top, int Left)
        {
            
return "<script>window.open('" + URL + "','" + WindowName + "','toolbar=" + Toolbar + ",')</script>";
        }

        
#endregion
原创粉丝点击