本文摘自PHP中文网,作者黄舟,侵删。
这篇文章主要介绍了C#常见应用函数,结合实例形式总结分析了C#常用的时间、URL、HTML、反射、小数运算等相关函数,需要的朋友可以参考下本文实例总结了C#常见应用函数。分享给大家供大家参考,具体如下:
1、页面写CS代码(代码内嵌)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <%@ Import Namespace= "System" %>
<%@ Import Namespace= "System.Collections.Generic" %>
<Script runat= "server" >
public int userId = 0;
protected void Page_Load( object sender, EventArgs e)
{
userId =Convert.ToInt32(Request.QueryString[ "UserID" ]);
Response.Write(userId);
}
</Script>
<%
if (userId > 0){
msg = "欢迎登录!" ;
}
else {
msg = "未找到用户" ;
}
%>
<%= this .msg %>
|
2、获取时间间隔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | /// <summary>
/// 获取时间间隔(模拟微博发布文章的时间间隔)
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public string GetDateStr(DateTime date)
{
if (date < DateTime.Now)
{
TimeSpan ts = DateTime.Now - date;
if (ts.TotalHours < 1 && ts.TotalMinutes < 1)
{
return "1分钟前" ;
}
else if (ts.TotalHours < 1 && ts.TotalMinutes > 0)
{
return Convert.ToInt32(ts.TotalMinutes) + "分钟前" ;
}
else if (ts.TotalHours < 4)
{
return Convert.ToInt32(ts.TotalHours) + "小时前" ;
}
else if (DateTime.Now.Date == date.Date)
{
return date.ToString( "HH:mm" );
}
else
{
return date.ToString( "yyyy-MM-dd" );
}
}
return date.ToString( "yyyy-MM-dd" );
}
|
3、遍历Url中的参数列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /// <summary>
/// 遍历Url中的参数列表
/// </summary>
/// <returns>如:(?userId=43&userType=2)</returns>
public string GetUrlParam()
{
string urlParam = "" ;
if (Request.QueryString.Count > 0)
{
urlParam = "?" ;
NameValueCollection keyVals = Request.QueryString;
foreach ( string key in keyVals.Keys)
{
urlParam += key + "=" + keyVals[key] + "&" ;
}
urlParam = urlParam.Substring(0, urlParam.LastIndexOf( '&' ));
}
return urlParam;
}
|
4、清除文本HTML码
阅读剩余部分
相关阅读 >>
C# 实现 cachehelper
C# winform webbrowser 设置为编辑模式的示例代码
具体介绍使用C#访问access数据库时,提示找不到可安装的isam(图)
C#相关面试题
C#学习记录:编写高质量代码改善整理建议4-8
C#之解决百度地图api app sn校验失败问题(代码实例)
C#devexpress gridcontrol日期行的显示格式设置详解(图文)
C#解析xml文件的代码实例分享
C#tuples(元组)
C#基础入门-关键字的介绍
更多相关阅读请进入《C#》频道 >>
清华大学出版社
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。
转载请注明出处:木庄网络博客 » 详解C#常见应用函数的实例总结