ASP.NET編程精選25種函數來源程式

來源:互聯網
上載者:User

   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、穿過Proxy 伺服器取遠端使用者真實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["變數"];

  以下是引用片段:

  用超連結傳送變數。

  如在任一頁中建超連結:點擊

  在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(字串變數,位元)——邏輯型

  以下是引用片段:

  查指定位置是否Null 字元;

  如:

  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.Replace("國","央"); //將國字換為央字

  Response.Write(str); //輸出結果為“中央”

  再如:(這個非常實用)

  string str="這是

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.