分类广告


推荐文章

  • 没有找到任何内容!
您当前的位置:中国站长下载网络编程NET专区 → 文章内容

c#.net函数列表

  • 作者:不详    来源:网络转载    发布时间:2008-2-28 8:32:33
  • 字体大小:

1、DateTime 数字型

System.DateTime currentTime=new System.DateTime();

1.1 取当前年月日时分秒 中国站长.站

currentTime=System.DateTime.Now; Chinaz_com

1.2 取当前年 Www^

int 年=currentTime.Year;

1.3 取当前月 站.长站

int 月=currentTime.Month;

Www_Chinaz_com

1.4 取当前日 中国站长_站,为中文网站提供动力

int 日=currentTime.Day;

中国.站长站

1.5 取当前时

中国站长_站,为中文网站提供动力

int 时=currentTime.Hour; 中国.站.长站

1.6 取当前分

Www.zhuye123.com

int 分=currentTime.Minute; 中.国.

1.7 取当前秒 站.长站

int 秒=currentTime.Second; 站长.站

1.8 取当前毫秒

int 毫秒=currentTime.Millisecond;

Chinaz~com

(变量可用中文)

中.国.

1.9 取中文日期显示——年月日时分 中国.站.长站

string strY=currentTime.ToString("f"); //不显示秒 Www_Chinaz_com

1.10 取中文日期显示_年月 Chinaz_com

string strYM=currentTime.ToString("y");

中国.站长站

1.11 取中文日期显示_月日

Chinaz

string strMD=currentTime.ToString("m");

1.12 取当前年月日,格式为:2003-9-23 中国.站长站

string strYMD=currentTime.ToString("d"); 中国站.长站

1.13 取当前时分,格式为:14:24 中国站长.站

string strT=currentTime.ToString("t"); Chinaz

2、Int32.Parse(变量) Int32.Parse("常量")

中国

字符型转换 转为32位数字型 Chinaz

3、 变量.ToString()

字符型转换 转为字符串 中.国.

12345.ToString("n"); //生成 12,345.00

中国.站.长站

12345.ToString("C"); //生成 ¥12,345.00 Chinaz_com

12345.ToString("e"); //生成 1.234500e+004 Www~Chinaz~com

12345.ToString("f4"); //生成 12345.0000 zhuye123.com

12345.ToString("x"); //生成 3039 (16进制) Www^

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

中国

4、变量.Length 数字型

取字串长度:

Www.zhuye123.com

如: string str="中国"; Www^

int Len = str.Length ; //Len是自定义变量, str是求测的字串的变量名

站长.站

5、System.Text.Encoding.Default.GetBytes(变量)

Www^

字码转换 转为比特码 Www^

如:byte[] bytStr = System.Text.Encoding.Default.GetBytes(str);

中.国站长站

然后可得到比特长度:

len = bytStr.Length;

6、System.Text.StringBuilder("") 中.国.站长站

字符串相加,(+号是不是也一样?) Www~Chinaz~com

如:System.Text.StringBuilder sb = new System.Text.StringBuilder(""); 中国.站长站

sb.Append("中华");

sb.Append("人民");

Www^

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地址:

Chinaz

以下为引用的内容:
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值; Chinaz~com

如,赋值: Session["username"]="小布什"; Chinaz

取值: Object objName=Session["username"];

中国.站.长站

String strName=objName.ToString(); 中国.站长站

清空: Session.RemoveAll(); Www_Chinaz_com

11、String str=Request.QueryString["变量"]; Www~Chinaz~com

用超链接传送变量。

[中国站长站]

如在任一页中建超链接:<a href=Edit.aspx?fbid=23>点击</a>

在Edit.aspx页中取值:String str=Request.QueryString["fdid"];

Chinaz~com

12、DOC对象.CreateElement("新建节点名");

创建XML文档新节点

13、父节点.AppendChild(子节点);

中国.站长站

将新建的子节点加到XML文档父节点下 中国

14、 父节点.RemoveChild(节点);

删除节点

15、Response

中国站长.站

Response.Write("字串");

中国

Response.Write(变量);

Www.zhuye123.com

向页面输出。 中国站长_站,为中文网站提供动力

Response.Redirect("URL地址");

跳转到URL指定的页面

Chinaz

16、char.IsWhiteSpce(字串变量,位数)——逻辑型

中国站长.站

查指定位置是否空字符;

如:

[中国站长站]

string str="中国 人民";

中国.站.长站

Response.Write(char.IsWhiteSpace(str,2)); //结果为:True, 第一个字符是0位,2是第三个字符。

站.长站

17、char.IsPunctuation('字符') --逻辑型 中.国.站长站

查字符是否是标点符号

Www.zhuye123.com

如:Response.Write(char.IsPunctuation('A')); //返回:False Chinaz

18、(int)'字符' 中.国站长站

把字符转为数字,查代码点,注意是单引号。 [中国站长站]

如:

中.国.

Response.Write((int)'中'); //结果为中字的代码:20013 中.国站长站

19、(char)代码

Www.zhuye123.com

把数字转为字符,查代码代表的字符。

中国站长_站,为中文网站提供动力

如:

Chinaz~com

Response.Write((char)22269); //返回“国”字。

中国.站长站

20、 Trim() zhuye123.com

清除字串前后空格 中国站长_站,为中文网站提供动力

21 、字串变量.Replace("子字串","替换为") 中.国站长站

字串替换

如:

Chinaz

string str="中国"; Www~Chinaz~com

str=str.Replace("国","央"); //将国字换为央字 中国站.长站

Response.Write(str); //输出结果为“中央” Chinaz~com

再如:(这个非常实用)

中国站长_站,为中文网站提供动力

以下为引用的内容:
string str="这是<script>脚本";
str=str.Replace("<","<font><</font>"); //将左尖括号替换为<font> 与 < 与 </font> (或换为<,但估计经XML存诸后,再提出仍会还原)
Response.Write(str); //显示为:“这是<script>脚本”
Www^

如果不替换,<script>将不显示,如果是一段脚本,将运行;而替换后,脚本将不运行。

Chinaz

这段代码的价值在于:你可以让一个文本中的所有HTML标签失效,全部显示出来,保护你的具有交互性的站点。

具体实现:将你的表单提交按钮脚本加上下面代码: 中国站.长站

string strSubmit=label1.Text; //label1是你让用户提交数据的控件ID。 中.国.

strSubmit=strSubmit.Replace("<","<font><</font>");

Chinaz

然后保存或输出strSubmit。

中.国站长站

用此方法还可以简单实现UBB代码。 中国站.长站

22、Math.Max(i,j)

中国站.长站

取i与j中的最大值 中国

如 int x=Math.Max(5,10); // x将取值 10

23、字串对比一般都用: if(str1==str2){ } , 但还有别的方法:

Www.zhuye123.com

(1)、string str1; str2

zhuye123.com

//语法:str1.EndsWith(str2); __检测字串str1是否以字串str2结尾,返回布尔值。如: 中国.站长站

if(str1.EndsWith(str2)){ Response.Write("字串str1是以"+str2+"结束的"); }

(2)、//语法:str1.Equals(str2); __检测字串str1是否与字串str2相等,返回布尔值,用法同上。 Www^

(3)、//语法 Equals(str1,str2); __检测字串str1是否与字串str2相等,返回布尔值,用法同上。 中国

24、IndexOf() 、LastIndexOf()

Www_Chinaz_com

查找字串中指定字符或字串首次(最后一次)出现的位置,返回索引值,如:

中国.站.长站

str1.IndexOf("字"); //查找“字”在str1中的索引值(位置) 中.国.站长站

str1.IndexOf("字串");//查找“字串”的第一个字符在str1中的索引值(位置)

zhuye123.com

str1.IndexOf("字串",3,2);//从str1第4个字符起,查找2个字符,查找“字串”的第一个字符在str1中的索引值(位置) Chinaz_com

25、Insert()

中国站.长站

在字串中指定索引位插入指定字符。如:

中国.站.长站

str1.Insert(1,"字");在str1的第二个字符处插入“字”,如果str1="中国",插入后为“中字国”;

中国.站.长站

26、PadLeft()、PadRight()

Www.zhuye123.com

在字串左(或右)加空格或指定char字符,使字串达到指定长度,如:

中国站长.站

以下为引用的内容:
<%
string str1="中国人";
str1=str1.PadLeft(10,'1'); //无第二参数为加空格
Response.Write(str1); //结果为“1111111中国人” , 字串长为10
%>
Www.zhuye123.com

27、Remove()

从指定位置开始删除指定数的字符

字串对比一般都用: if(str1==str2){ } , 但还有别的方法: [中国站长站]

1、string str1; str2

中.国.

//语法: str1.EndsWith(str2); __检测字串str1是否以字串str2结尾,返回布尔值。如:

Chinaz

if(str1.EndsWith(str2)){ Response.Write("字串str1是以"+str2+"结束的"); } 中.国.

2、//语法:str1.Equals(str2); __检测字串str1是否与字串str2相等,返回布尔值,用法同上。 中国站.长站

3、//语法 Equals(str1,str2); __检测字串str1是否与字串str2相等,返回布尔值,用法同上。

28、IndexOf() Www_Chinaz_com

查找字串中指定字符或字串首次出现的位置,返首索引值,如:

str1.IndexOf("字"); //查找“字”在str1中的索引值(位置)

站长.站

str1.IndexOf("字串");//查找“字串”的第一个字符在str1中的索引值(位置)

Www.zhuye123.com

str1.IndexOf("字串",3,2);//从str1第4个字符起,查找2个字符,查找“字串”的第一个字符在str1中的索引值(位置)