C#常用代码
1
1、DateTime 数字型
2
System.DateTime currentTime=new System.DateTime();
3
1.1 取当前年月日时分秒
4
currentTime=System.DateTime.Now;
5
1.2 取当前年
6
int 年=currentTime.Year;
7
1.3 取当前月
8
int 月=currentTime.Month;
9
1.4 取当前日
10
int 日=currentTime.Day;
11
1.5 取当前时
12
int 时=currentTime.Hour;
13
1.6 取当前分
14
int 分=currentTime.Minute;
15
1.7 取当前秒
16
int 秒=currentTime.Second;
17
1.8 取当前毫秒
18
int 毫秒=currentTime.Millisecond;
19
(变量可用中文)
20
21
2、Int32.Parse(变量) Int32.Parse(“常量“)
22
字符型转换 转为32位数字型
23
24
3、 变量.ToString()
25
字符型转换 转为字符串
26
12345.ToString(“n“); //生成 12,345.00
27
12345.ToString(“C“); //生成 ¥12,345.00
28
12345.ToString(“e“); //生成 1.234500e+004
29
12345.ToString(“f4“); //生成 12345.0000
30
12345.ToString(“x“); //生成 3039 (16进制)
31
12345.ToString(“p“); //生成 1,234,500.00%
32
33
34
4、变量.Length 数字型
35
取字串长度:
36
如: string str=“中国“;
37
int Len = str.Length ; //Len是自定义变量, str是求测的字串的变量名
38
39
5、System.Text.Encoding.Default.GetBytes(变量)
40
字码转换 转为比特码
41
如:byte[] bytStr = System.Text.Encoding.Default.GetBytes(str);
42
然后可得到比特长度:
43
len = bytStr.Length;
44
45
6、System.Text.StringBuilder(“”)
46
字符串相加,(+号是不是也一样?)
47
如:System.Text.StringBuilder sb = new System.Text.StringBuilder(“”);
48
sb.Append(“中华“);
49
sb.Append(“人民“);
50
sb.Append(“共和国“);
51
52
7、变量.Substring(参数1,参数2);
53
截取字串的一部分,参数1为左起始位数,参数2为截取几位。
54
如:string s1 = str.Substring(0,2);
55
56
8、String user_IP=Request.ServerVariables[“REMOTE_ADDR“].ToString();
57
取远程用户IP地址
58
59
9、穿过代理服务器取远程用户真实IP地址:
60
if(Request.ServerVariables[“HTTP_VIA“]!=null)
{
61
string user_IP=Request.ServerVariables[“HTTP_X_FORWARDED_FOR“].ToString();
62
}else
{
63
string user_IP=Request.ServerVariables[“REMOTE_ADDR“].ToString();
64
}
65
66
10、 Session[“变量“];
67
存取Session值;
68
如,赋值: Session[“username“]=“小布什“;
69
70
取值: Object objName=Session[“username“];
71
String strName=objName.ToString();
72
清空: Session.RemoveAll();
73
74
11、String str=Request.QueryString[“变量“];
75
用超链接传送变量。
76
如在任一页中建超链接:<a href=Edit.aspx?fbid=23>点击</a>
77
在Edit.aspx页中取值:String str=Request.QueryString[“fdid“];
78
79
12、DOC对象.CreateElement(“新建节点名“);
80
创建XML文档新节点
81
82
13、父节点.AppendChild(子节点);
83
将新建的子节点加到XML文档父节点下
84
85
14、 父节点.RemoveChild(节点);
86
删除节点
87
88
15、Response
89
Response.Write(“字串“);
90
Response.Write(变量);
91
向页面输出。
92
93
Response.Redirect(“URL地址“);
94
跳转到URL指定的页面
95
96
16、char.IsWhiteSpce(字串变量,位数)——逻辑型
97
查指定位置是否空字符;
98
如:
99
string str=“中国 人民“;
100
Response.Write(char.IsWhiteSpace(str,2)); //结果为:True, 第一个字符是0位,2是第三个字符。
101
102
17、char.IsPunctuation(\’字符\’) —逻辑型
103
查字符是否是标点符号
104
如:Response.Write(char.IsPunctuation(\’A\’)); //返回:False
105
106
18、(int)\’字符\’
107
把字符转为数字,查代码点,注意是单引号。
108
如:
109
Response.Write((int)\’中\’); //结果为中字的代码:20013
110
111
19、(char)代码
112
把数字转为字符,查代码代表的字符。
113
如:
114
Response.Write((char)22269); //返回“国”字。
115
116
20、 Trim()
117
清除字串前后空格
118
119
21 、字串变量.Replace(“子字串“,“替换为“)
120
字串替换
121
如:
122
string str=“中国“;
123
str=str.Replace(“国“,“央“); //将国字换为央字
124
Response.Write(str); //输出结果为“中央”
125
126
再如:(这个非常实用)
127
128
string str=“这是<script>脚本“;
129
str=str.Replace(“<“,“<font><</font>“); //将左尖括号替换为<font> 与 < 与 </font> (或换为<,但估计经XML存诸后,再提出仍会还原)
130
Response.Write(str); //显示为:“这是<script>脚本”
131
132
如果不替换,<script>将不显示,如果是一段脚本,将运行;而替换后,脚本将不运行。
133
这段代码的价值在于:你可以让一个文本中的所有HTML标签失效,全部显示出来,保护你的具有交互性的站点。
134
具体实现:将你的表单提交按钮脚本加上下面代码:
135
string strSubmit=label1.Text; //label1是你让用户提交数据的控件ID。
136
strSubmit=strSubmit.Replace(“<“,“<font><</font>“);
137
然后保存或输出strSubmit。
138
用此方法还可以简单实现UBB代码。
139
140
22、Math.Max(i,j)
141
取i与j中的最大值
142
如 int x=Math.Max(5,10); // x将取值 10
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
版权声明:本文为studio313原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。