欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net常用函数收藏第1/2页

程序员文章站 2022-04-14 19:16:54
/// /// 得到站点用户ip /// ///
/// <summary>
/// 得到站点用户ip
/// </summary>
/// <returns></returns>
public static string getuserip()
{
return httpcontext.current.request.servervariables["remote_addr"].tostring();
}

/// <summary>
/// 去除字符串最后一个','号
/// </summary>
/// <param name="chr">:要做处理的字符串</param>
/// <returns>返回已处理的字符串</returns>
public static string lost(string chr)
{
if (chr == null || chr == string.empty)
{
return "";
}
else
{
chr = chr.remove(chr.lastindexof(","));
return chr;
}
}

/// <summary>
/// 去除字符串第一个'/'号
/// </summary>
/// <param name="chr">要做处理的字符串</param>
/// <returns>返回已处理的字符串</returns>
public static string lostfirst(string chr)
{
string flg = "";
if (chr != string.empty || chr != null)
{
if (chr.substring(0, 1) == "/")
flg = chr.replace(chr.substring(0, 1), "");
else
flg = chr;
}
return flg;
}

/// <summary>
/// 替换html中的特殊字符
/// </summary>
/// <param name="thestring">需要进行替换的文本。</param>
/// <returns>替换完的文本。</returns>
public static string htmlencode(string thestring)
{
thestring = thestring.replace(">", ">");
thestring = thestring.replace("<", "<");
thestring = thestring.replace(" ", " ");
thestring = thestring.replace(" ", " ");
thestring = thestring.replace("\"", """);
thestring = thestring.replace("\'", "'");
thestring = thestring.replace("\n", "<br/> ");
return thestring;
}

/// <summary>
/// 恢复html中的特殊字符
/// </summary>
/// <param name="thestring">需要恢复的文本。</param>
/// <returns>恢复好的文本。</returns>
public static string htmldiscode(string thestring)
{
thestring = thestring.replace(">", ">");
thestring = thestring.replace("<", "<");
thestring = thestring.replace(" ", " ");
thestring = thestring.replace(" ", " ");
thestring = thestring.replace(""", "\"");
thestring = thestring.replace("'", "\'");
thestring = thestring.replace("<br/> ", "\n");
return thestring;
}



/// <summary>
/// 生成随机数字
/// </summary>
/// <param name="length">生成长度</param>
/// <returns></returns>
public static string number(int length)
{
return number(length, false);
}

/// <summary>
/// 生成随机数字
/// </summary>
/// <param name="length">生成长度</param>
/// <param name="sleep">是否要在生成前将当前线程阻止以避免重复</param>
/// <returns></returns>
public static string number(int length, bool sleep)
{
if (sleep)
system.threading.thread.sleep(3);
string result = "";
system.random random = new random();
for (int i = 0; i < length; i++)
{
result += random.next(10).tostring();
}
return result;
}
1