整理出C#(.net)的常用函數(shù)和方法集
2024-07-10 13:11:53
供稿:網(wǎng)友
1、datetime 數(shù)字型system.datetime currenttime=new system.datetime();
1.1 取當(dāng)前年月日時分秒
currenttime=system.datetime.now;
1.2 取當(dāng)前年
int 年=currenttime.year;
1.3 取當(dāng)前月
int 月=currenttime.month;
1.4 取當(dāng)前日
int 日=currenttime.day;
1.5 取當(dāng)前時
int 時=currenttime.hour;
1.6 取當(dāng)前分
int 分=currenttime.minute;
1.7 取當(dāng)前秒
int 秒=currenttime.second;
1.8 取當(dāng)前毫秒
int 毫秒=currenttime.millisecond;
(變量可用中文)
1.9 取中文日期顯示——年月日時分
string stry=currenttime.tostring("f"); //不顯示秒
1.10 取中文日期顯示_年月
string strym=currenttime.tostring("y");
1.11 取中文日期顯示_月日
string strmd=currenttime.tostring("m");
1.12 取中文年月日
string strymd=currenttime.tostring("d");
1.13 取當(dāng)前時分,格式為:14:24
string strt=currenttime.tostring("t");
1.14 取當(dāng)前時間,格式為:2003-09-23t14:46:48
string strt=currenttime.tostring("s");
1.15 取當(dāng)前時間,格式為:2003-09-23 14:48:30z
string strt=currenttime.tostring("u");
1.16 取當(dāng)前時間,格式為:2003-09-23 14:48
string strt=currenttime.tostring("g");
1.17 取當(dāng)前時間,格式為:tue, 23 sep 2003 14:52:40 gmt
string strt=currenttime.tostring("r");
1.18獲得當(dāng)前時間 n 天后的日期時間
datetime newday = datetime.now.adddays(100);
2、int32.parse(變量) int32.parse("常量") 字符型轉(zhuǎn)換 轉(zhuǎn)為32位數(shù)字型
3、變量.tostring() 字符型轉(zhuǎn)換 轉(zhuǎn)為字符串
12345.tostring("n"); //生成 12,345.00
12345.tostring("c"); //生成 ¥12,345.00
12345.tostring("e"); //生成 1.234500e+004
12345.tostring("f4"); //生成 12345.0000
12345.tostring("x"); //生成 3039 (16進(jìn)制)
12345.tostring("p"); //生成 1,234,500.00%
4、變量.length 數(shù)字型取字串長度:
如: string str="中國";
int len = str.length ; //len是自定義變量, str是求測的字串的變量名
5、system.text.encoding.default.getbytes(變量) 字碼轉(zhuǎn)換 轉(zhuǎn)為比特碼
如:byte[] bytstr = system.text.encoding.default.getbytes(str);
然后可得到比特長度:
len = bytstr.length;
6、system.text.stringbuilder("") 字符串相加,(+號是不是也一樣?)
如:system.text.stringbuilder sb = new system.text.stringbuilder("");
sb.append("中華");
sb.append("人民");
sb.append("共和國");
7、變量.substring(參數(shù)1,參數(shù)2); 截取字串的一部分,參數(shù)1為左起始位數(shù),參數(shù)2為截取幾位。
如:string s1 = str.substring(0,2);
8、string user_ip=request.servervariables["remote_addr"].tostring(); 取遠(yuǎn)程用戶ip地址
9、穿過代理服務(wù)器取遠(yuǎn)程用戶真實(shí)ip地址:if(request.servervariables["http_via"]!=null){
string user_ip=request.servervariables["http_x_forwarded_for"].tostring();
}else{
string user_ip=request.servervariables["remote_addr"].tostring();
}
10、 session["變量"]; 存取session值;
如,賦值: session["username"]="小布什";
取值: object objname=session["username"];
string strname=objname.tostring();
清空: session.removeall();
11、string str=request.querystring["變量"]; 用超鏈接傳送變量。
如在任一頁中建超鏈接:<a href=edit.aspx?fbid=23>點(diǎn)擊</a>
在edit.aspx頁中取值:string str=request.querystring["fdid"];
12、doc對象.createelement("新建節(jié)點(diǎn)名"); 創(chuàng)建xml文檔新節(jié)點(diǎn)
13、父節(jié)點(diǎn).appendchild(子節(jié)點(diǎn));將新建的子節(jié)點(diǎn)加到xml文檔父節(jié)點(diǎn)下
14、父節(jié)點(diǎn).removechild(節(jié)點(diǎn)); 刪除節(jié)點(diǎn)
15、response response.write("字串");
response.write(變量);
向頁面輸出。
response.redirect("url地址");
跳轉(zhuǎn)到url指定的頁面
16、char.iswhitespce(字串變量,位數(shù))——邏輯型查指定位置是否空字符;
如:
string str="中國 人民";
response.write(char.iswhitespace(str,2)); //結(jié)果為:true, 第一個字符是0位,2是第三個字符。
17、char.ispunctuation('字符') --邏輯型查字符是否是標(biāo)點(diǎn)符號
如:response.write(char.ispunctuation('a')); //返回:false
18、(int)'字符' 把字符轉(zhuǎn)為數(shù)字,查代碼點(diǎn),注意是單引號。
如:
response.write((int)'中'); //結(jié)果為中字的代碼:20013
19、(char)代碼把數(shù)字轉(zhuǎn)為字符,查代碼代表的字符。
如:
response.write((char)22269); //返回“國”字。
20、 trim() 清除字串前后空格
21 、字串變量.replace("子字串","替換為") 字串替換
如:
string str="中國";
str=str.replace("國","央"); //將國字換為央字
response.write(str); //輸出結(jié)果為“中央”
再如:(這個非常實(shí)用)
string str="這是<script>腳本";
str=str.replace("<","<font><</font>"); //將左尖括號替換為<font> 與 < 與 </font> (或換為<,但估計(jì)經(jīng)xml存諸后,再提出仍會還原)
response.write(str); //顯示為:“這是<script>腳本”
如果不替換,<script>將不顯示,如果是一段腳本,將運(yùn)行;而替換后,腳本將不運(yùn)行。
這段代碼的價值在于:你可以讓一個文本中的所有html標(biāo)簽失效,全部顯示出來,保護(hù)你的具有交互性的站點(diǎn)。
具體實(shí)現(xiàn):將你的表單提交按鈕腳本加上下面代碼:
string strsubmit=label1.text; //label1是你讓用戶提交數(shù)據(jù)的控件id。
strsubmit=strsubmit.replace("<","<font><</font>");
然后保存或輸出strsubmit。
用此方法還可以簡單實(shí)現(xiàn)ubb代碼。
22、math.max(i,j) 取i與j中的最大值
如 int x=math.max(5,10); // x將取值 10
23、字串對比一般都用: if(str1==str2){ } , 但還有別的方法:
(1)、
string str1; str2
//語法: str1.endswith(str2); __檢測字串str1是否以字串str2結(jié)尾,返回布爾值.如:
if(str1.endswith(str2)){ response.write("字串str1是以"+str2+"結(jié)束的"); }
(2)、
//語法:str1.equals(str2); __檢測字串str1是否與字串str2相等,返回布爾值,用法同上.
(3)、
//語法 equals(str1,str2); __檢測字串str1是否與字串str2相等,返回布爾值,用法同上.
24、indexof() 、lastindexof() 查找字串中指定字符或字串首次(最后一次)出現(xiàn)的位置,返回索引值,如:
str1.indexof("字"); //查找“字”在str1中的索引值(位置)
str1.indexof("字串");//查找“字串”的第一個字符在str1中的索引值(位置)
str1.indexof("字串",3,2);//從str1第4個字符起,查找2個字符,查找“字串”的第一個字符在str1中的索引值(位置)
25、insert() 在字串中指定索引位插入指定字符。如:
str1.insert(1,"字");在str1的第二個字符處插入“字”,如果str1="中國",插入后為“中字國”;
26、padleft()、padright() 在字串左(或右)加空格或指定char字符,使字串達(dá)到指定長度,如:
<%
string str1="中國人";
str1=str1.padleft(10,'1'); //無第二參數(shù)為加空格
response.write(str1); //結(jié)果為“1111111中國人” , 字串長為10
%>
27、remove() 從指定位置開始刪除指定數(shù)的字符
<%
string str1="我是薩達(dá)姆的崇拜者之一";
response.write(str1.remove(5,4)); //結(jié)果為“我是薩達(dá)姆之一”
%>