一些C#实用的方法汇总

代码如下:

一些C#实用的方法汇总
  1  /// <summary>
  2     /// 过滤字符串方法,用于将单引号等特殊符号转化成中文符号
  3     /// </summary>
  4     /// <param name="msg">要转化的字符串</param>
  5     public static string  FilterStr(string msg) 
  6     {
  7         string result = "";
  8        
  9        // msg = msg.Replace(",", ",");       
 10         //msg = msg.Replace("<", "<");
 11         //msg = msg.Replace(">", ">");
 12        // msg = msg.Replace("\n", "<br>");
 13         // msg = msg.Replace("\"", """);
 14 
 15         msg = msg.Trim();
 16         msg = msg.Replace("", "‘‘");
 17     
 18         result = msg;
 19 
 20         return result;
 21     }
 22 
 23 
 24     public static void message(string meg)
 25     {
 26       
 27     }
 28 
 29     public static string FilterStrHtml(string msg)
 30     {
 31         string result = "";
 32 
 33         
 34         msg = msg.Replace("\‘", "");
 35         //msg = msg.Replace(",", ",");
 36         msg = msg.Replace("\n", "<br>");
 37         result = msg;
 38 
 39         return result;
 40     }
 41 
 42     public static string FilterStrnocode(string msg)
 43     {
 44         string result = "";
 45 
 46 
 47         msg = msg.Replace("", "\‘");
 48         msg = msg.Replace("<br>", "\n");
 49         result = msg;
 50 
 51         return result;
 52     }
 53 
 54 
 55 
 56     //弹出消息框
 57     /// <summary>
 58     /// 消息提示框
 59     /// </summary>
 60     /// <param name="msg">消息内容</param>
 61     public static void ShowMsgBox(string msg) 
 62     {
 63        System.Web.HttpContext.Current.Response.Write("<script>alert(‘"+msg+"‘)</script>");
 64     }
 65 
 66     /// <summary>
 67     /// 弹出消息框,调用弹出消息框,不影响样式buss.Showmessage(this.Page,"提交成功");
 68     /// </summary>
 69     /// <param name="Page">Page对像</param>
 70     /// <param name="msg">消息</param>
 71     public static void Showmessage(System.Web.UI.Page Page, string msg)
 72     {
 73         //ClientScript.RegisterStartupScript(GetType(), "JS1", "alert(‘" + msg + "‘)", true); 
 74         // Page.ClientScript.RegisterStartupScript(Page.GetType(), "JS1", "alert(‘" + msg + "‘)", true); 
 75         Page.ClientScript.RegisterStartupScript(Page.GetType(), "", " <script lanuage=javascript>alert(‘" + msg + "‘)</script>");
 76     }
 77 
 78     /// <summary>
 79     /// 截取字符串
 80     /// </summary>
 81     /// <param name="msg">原字符串</param>
 82     /// <param name="lenth">要截取的长度</param>
 83     /// <returns></returns>
 84     public static string ReturnStr(string msg, int lenth)
 85     {
 86         string result = msg;
 87 
 88         if (msg.Length > lenth)
 89         {
 90             result = result.Substring(0, lenth);
 91         }
 92         // http://www.cnblogs.com/sosoft/
 93         return result;
 94 
 95     }
 96     /// <summary>
 97     /// 
 98     /// </summary>
 99     /// <param name="sqlstr"></param>
100     /// <returns></returns>
101 
102     /// <summary>
103     /// 剪切指定长度的字符串,并去掉HTML标记
104     /// </summary>
105     /// <param name="strr">要剪切字符串的Object形式</param>
106     /// <param name="len">长度(中文长度为2)</param>
107     /// <returns></returns>
108     public static string CutStringX(object strr, int len)
109     {
110         string str = strr.ToString();
111         string strRet = "";
112         char CH;
113         int nLen = str.Length;
114         int nCutLen = 0;
115         int nPos = 0;
116         int bLeft = 0;
117         int nChinese = 0;
118         while (nPos < nLen && nCutLen < len)
119         {
120             CH = str[nPos];
121             nPos++;
122 
123             if (CH == <)
124             {
125                 bLeft++;
126                 continue;
127             }
128             if (CH == >)
129             {
130                 bLeft--;
131                 continue;
132             }
133             if (nCutLen == 0 && CH.ToString() == " " && CH.ToString() == "\n")
134             {
135                 continue;
136             }
137             if (bLeft == 0)
138             {
139                 //是否为中文
140                 if (IsChinese(CH))
141                 {
142                     nCutLen += 2;
143                     nChinese++;
144                 }
145                 else
146                 {
147                     nCutLen += 1;
148                 }
149                 strRet += CH;
150             }
151         }
152         strRet = strRet.Replace(" ", " ");
153         if (nPos < nLen)
154         {
155             strRet += "";
156            // strRet += "...";
157         }
158         return strRet;
159     }
160 
161 
162     //是否为中文
163     public static bool IsChinese(char ch)
164     {
165         ASCIIEncoding en = new ASCIIEncoding();
166         byte[] b = en.GetBytes(ch.ToString());
167         return (b[0] == 63);
168     }
169 
170     //HTML标记转换
171     public static string HTMLEncode(string str)
172     {
173         str = str.Replace("<", "<");
174         str = str.Replace(">", ">");
175         str = str.Replace("\n", "<br/>");
176 
177         return str;
178     }
179 
180 
181     /// <summary>
182     /// 把日期时间转换为日期,去掉时间
183     /// </summary>
184     /// <param name="str">格式:YYYY-MM-DD HH:MM:SS</param>
185     /// <returns>返回日期 YYYY-MM-DD</returns>
186     public static string GetDate(string str)
187     {
188         string[] s = str.Split( );
189         string[] ss = s[0].Split(-);
190         if (ss[1].Length < 2)
191             ss[1] = "0" + ss[1];
192         if (ss[2].Length < 2)
193             ss[2] = "0" + ss[2];
194         return ss[0] + "-" + ss[1] + "-" + ss[2];
195     }
196 
197  //随机函数产生字符   
198     public static string CreateRandomCode(int codeCount, string allChar)
199     {
200         //验证码中的出现的字符,避免了一些容易混淆的字符。 
201         if (string.IsNullOrEmpty(allChar)) allChar = "3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,T,U,W,X,Y";
202         string[] allCharArray = allChar.Split(,);
203         string randomCode = "";
204         int temp = -1;
205         bool breCreate = (codeCount < 6 && allCharArray.Length > 15);
206 
207         Random rand = new Random();
208         for (int i = 0; i < codeCount; i++)
209         {
210             if (temp != -1)
211             {
212                 rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
213             }
214             int t = rand.Next(allCharArray.Length);
215             if (temp == t && breCreate)
216             {
217                 return CreateRandomCode(codeCount, allChar);
218             }
219             temp = t;
220             randomCode += allCharArray[t];
221         }
222         return randomCode;
223     }
一些C#实用的方法汇总

一些C#实用的方法汇总

上一篇:Delphi数学运算当中四舍五入的问题


下一篇:替换