一些C#实用的方法汇总

王朝学院·作者佚名  2016-08-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

/// <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(0, 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 = 0;

int nPos = 0;

int bLeft = 0;

int nChinese = 0;

while (nPos < nLen && nCutLen < len)

{

CH = str[nPos];

nPos++;

if (CH == '<')

{

bLeft++;

continue;

}

if (CH == '>')

{

bLeft--;

continue;

}

if (nCutLen == 0 && CH.ToString() == " " && CH.ToString() == "\n")

{

continue;

}

if (bLeft == 0)

{

//是否为中文

if (IsChinese(CH))

{

nCutLen += 2;

nChinese++;

}

else

{

nCutLen += 1;

}

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[0] == 63);

}

//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[0].Split('-');

if (ss[1].Length < 2)

ss[1] = "0" + ss[1];

if (ss[2].Length < 2)

ss[2] = "0" + ss[2];

return ss[0] + "-" + ss[1] + "-" + ss[2];

}

//随机函数产生字符

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 = -1;

bool breCreate = (codeCount < 6 && allCharArray.Length > 15);

Random rand = new Random();

for (int i = 0; i < codeCount; i++)

{

if (temp != -1)

{

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;

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航