天天看點

C#常用函數方法Set

1、datetime 數字型

   system.datetime currenttime=new system.datetime();;

  1.1 取目前年月日時分秒

   currenttime=system.datetime.now;;

  1.2 取目前年

   int 年=currenttime.year;;

  1.3 取目前月

   int 月=currenttime.month;;

  1.4 取目前日

   int 日=currenttime.day;;

  1.5 取目前時

   int 時=currenttime.hour;;

  1.6 取目前分

   int 分=currenttime.minute;;

  1.7 取目前秒

   int 秒=currenttime.second;;

  1.8 取目前毫秒

   int 毫秒=currenttime.millisecond;;

   (變量可用中文)

2、int32.parse(變量) int32.parse("常量")

   字元型轉換 轉為32位數字型

3、 變量.tostring()

   字元型轉換 轉為字元串

   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進制)

   12345.tostring("p");; //生成 1,234,500.00%

4、變量.length 數字型

   取字串長度:

   如: string str="中國";;

   int len = str.length ;; //len是自定義變量, str是求測的字串的變量名

5、system.text.encoding.default.getbytes(變量)

   字碼轉換 轉為比特碼

   如: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(參數1,參數2);;

   截取字串的一部分,參數1為左起始位數,參數2為截取幾位。

   如:string s1 = str.substring(0,2);;

8、string user_ip=request.servervariables["remote_addr"].tostring();;

   取遠端使用者ip位址

9、穿過代理伺服器取遠端使用者真實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〉點選〈/a〉

   在edit.aspx頁中取值:string str=request.querystring["fdid"];;

12、doc對象.createelement("建立節點名");;

   建立xml文檔新節點

13、父節點.appendchild(子節點);

   将建立的子節點加到xml文檔父節點下

14、 父節點.removechild(節點);;

   删除節點

15、response

   response.write("字串");

   response.write(變量);

   向頁面輸出。

   response.redirect("url位址");

   跳轉到url指定的頁面

16、char.iswhitespce(字串變量,位數)——邏輯型

   查指定位置是否空字元;

   如:

   string str="中國 人民";;

   response.write(char.iswhitespace(str,2));; //結果為:true, 第一個字元是0位,2是第三個字元。

17、char.ispunctuation('字元') --邏輯型

   查字元是否是标點符号

   如:response.write(char.ispunctuation('a'));; //傳回:false

18、(int)'字元'

   把字元轉為數字,查代碼點,注意是單引号。

   response.write((int)'中');; //結果為中字的代碼:20013

19、(char)代碼

   把數字轉為字元,查代碼代表的字元。

   response.write((char)22269);; //傳回;國”字。

20、 trim()

   清除字串前後空格

21 、字串變量.replace("子字串","替換為")

   字串替換

   string str="中國";;

   str=str.replac