一些C#实用的方法汇总

时间:2021-08-21 06:41:00

代码如下:

  /// <summary>
/// 过滤字符串方法,用于将单引号等特殊符号转化成中文符号
/// </summary>
/// <param name="msg">要转化的字符串</param>
public static string FilterStr(string msg)
{
string result = ""; // msg = msg.Replace(",", ",");
//msg = msg.Replace("<", "<");
//msg = msg.Replace(">", ">");
// msg = msg.Replace("\n", "<br>");
// msg = msg.Replace("\"", """); msg = msg.Trim();
msg = msg.Replace("'", "''"); result = msg; return result;
} public static void message(string meg)
{ } public static string FilterStrHtml(string msg)
{
string result = ""; msg = msg.Replace("\'", "'");
//msg = msg.Replace(",", ",");
msg = msg.Replace("\n", "<br>");
result = msg; return result;
} public static string FilterStrnocode(string msg)
{
string result = ""; msg = msg.Replace("'", "\'");
msg = msg.Replace("<br>", "\n");
result = msg; return result;
} //弹出消息框
/// <summary>
/// 消息提示框
/// </summary>
/// <param name="msg">消息内容</param>
public static void ShowMsgBox(string msg)
{
System.Web.HttpContext.Current.Response.Write("<script>alert('"+msg+"')</script>");
} /// <summary>
/// 弹出消息框,调用弹出消息框,不影响样式buss.Showmessage(this.Page,"提交成功");
/// </summary>
/// <param name="Page">Page对像</param>
/// <param name="msg">消息</param>
public static void Showmessage(System.Web.UI.Page Page, string msg)
{
//ClientScript.RegisterStartupScript(GetType(), "JS1", "alert('" + msg + "')", true);
// Page.ClientScript.RegisterStartupScript(Page.GetType(), "JS1", "alert('" + msg + "')", true);
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", " <script lanuage=javascript>alert('" + msg + "')</script>");
} /// <summary>
/// 截取字符串
/// </summary>
/// <param name="msg">原字符串</param>
/// <param name="lenth">要截取的长度</param>
/// <returns></returns>
public static string ReturnStr(string msg, int lenth)
{
string result = msg; if (msg.Length > lenth)
{
result = result.Substring(, lenth);
}
// http://www.cnblogs.com/sosoft/
return result; }
/// <summary>
///
/// </summary>
/// <param name="sqlstr"></param>
/// <returns></returns> /// <summary>
/// 剪切指定长度的字符串,并去掉HTML标记
/// </summary>
/// <param name="strr">要剪切字符串的Object形式</param>
/// <param name="len">长度(中文长度为2)</param>
/// <returns></returns>
public static string CutStringX(object strr, int len)
{
string str = strr.ToString();
string strRet = "";
char CH;
int nLen = str.Length;
int nCutLen = ;
int nPos = ;
int bLeft = ;
int nChinese = ;
while (nPos < nLen && nCutLen < len)
{
CH = str[nPos];
nPos++; if (CH == '<')
{
bLeft++;
continue;
}
if (CH == '>')
{
bLeft--;
continue;
}
if (nCutLen == && CH.ToString() == " " && CH.ToString() == "\n")
{
continue;
}
if (bLeft == )
{
//是否为中文
if (IsChinese(CH))
{
nCutLen += ;
nChinese++;
}
else
{
nCutLen += ;
}
strRet += CH;
}
}
strRet = strRet.Replace(" ", " ");
if (nPos < nLen)
{
strRet += "";
// strRet += "...";
}
return strRet;
} //是否为中文
public static bool IsChinese(char ch)
{
ASCIIEncoding en = new ASCIIEncoding();
byte[] b = en.GetBytes(ch.ToString());
return (b[] == );
} //HTML标记转换
public static string HTMLEncode(string str)
{
str = str.Replace("<", "<");
str = str.Replace(">", ">");
str = str.Replace("\n", "<br/>"); return str;
} /// <summary>
/// 把日期时间转换为日期,去掉时间
/// </summary>
/// <param name="str">格式:YYYY-MM-DD HH:MM:SS</param>
/// <returns>返回日期 YYYY-MM-DD</returns>
public static string GetDate(string str)
{
string[] s = str.Split(' ');
string[] ss = s[].Split('-');
if (ss[].Length < )
ss[] = "" + ss[];
if (ss[].Length < )
ss[] = "" + ss[];
return ss[] + "-" + ss[] + "-" + ss[];
} //随机函数产生字符
public static string CreateRandomCode(int codeCount, string allChar)
{
//验证码中的出现的字符,避免了一些容易混淆的字符。
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";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -;
bool breCreate = (codeCount < && allCharArray.Length > ); Random rand = new Random();
for (int i = ; i < codeCount; i++)
{
if (temp != -)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(allCharArray.Length);
if (temp == t && breCreate)
{
return CreateRandomCode(codeCount, allChar);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}