国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

C#格式化日期時(shí)間

2024-07-21 02:30:23
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

DateTime dt = DateTime.Now;
Label1.Text = dt.ToString();//2005-11-5 13:21:25
Label2.Text = dt.ToFileTime().ToString();//127756416859912816
Label3.Text = dt.ToFileTimeUtc().ToString();//127756704859912816
Label4.Text = dt.ToLocalTime().ToString();//2005-11-5 21:21:25
Label5.Text = dt.ToLongDateString().ToString();//2005年11月5日
Label6.Text = dt.ToLongTimeString().ToString();//13:21:25
Label7.Text = dt.ToOADate().ToString();//38661.5565508218
Label8.Text = dt.ToShortDateString().ToString();//2005-11-5
Label9.Text = dt.ToShortTimeString().ToString();//13:21
Label10.Text = dt.ToUniversalTime().ToString();//2005-11-5 5:21:25
?2005-11-5 13:30:28.4412864
Label1.Text = dt.Year.ToString();//2005
Label2.Text = dt.Date.ToString();//2005-11-5 0:00:00
Label3.Text = dt.DayOfWeek.ToString();//Saturday
Label4.Text = dt.DayOfYear.ToString();//309
Label5.Text = dt.Hour.ToString();//13
Label6.Text = dt.Millisecond.ToString();//441
Label7.Text = dt.Minute.ToString();//30
Label8.Text = dt.Month.ToString();//11
Label9.Text = dt.Second.ToString();//28
Label10.Text = dt.Ticks.ToString();//632667942284412864
Label11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864
Label1.Text = dt.ToString();//2005-11-5 13:47:04
Label2.Text = dt.AddYears(1).ToString();//2006-11-5 13:47:04
Label3.Text = dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
Label4.Text = dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
Label5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
Label6.Text = dt.AddMonths(1).ToString();//2005-12-5 13:47:04
Label7.Text = dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
Label8.Text = dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
Label9.Text = dt.AddTicks(1000).ToString();//2005-11-5 13:47:04
Label10.Text = dt.CompareTo(dt).ToString();//0
//Label11.Text = dt.Add(?).ToString();//問(wèn)號(hào)為一個(gè)時(shí)間段
Label1.Text = dt.Equals("2005-11-6 16:11:04").ToString();//False
Label2.Text = dt.Equals(dt).ToString();//True
Label3.Text = dt.GetHashCode().ToString();//1474088234
Label4.Text = dt.GetType().ToString();//System.DateTime
Label5.Text = dt.GetTypeCode().ToString();//DateTime

Label1.Text = dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25
Label2.Text = dt.GetDateTimeFormats('t')[0].ToString();//14:06
Label3.Text = dt.GetDateTimeFormats('y')[0].ToString();//2005年11月
Label4.Text = dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日
Label5.Text = dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
Label6.Text = dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05
Label7.Text = dt.GetDateTimeFormats('D')[3].ToString();//星期六 2005年11月5日
Label8.Text = dt.GetDateTimeFormats('M')[0].ToString();//11月5日
Label9.Text = dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 14:06
Label10.Text = dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06
Label11.Text = dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT

Label1.Text =? string.Format("{0:d}",dt);//2005-11-5
Label2.Text =? string.Format("{0:D}",dt);//2005年11月5日
Label3.Text =? string.Format("{0:f}",dt);//2005年11月5日 14:23
Label4.Text =? string.Format("{0:F}",dt);//2005年11月5日 14:23:23
Label5.Text =? string.Format("{0:g}",dt);//2005-11-5 14:23
Label6.Text =? string.Format("{0:G}",dt);//2005-11-5 14:23:23
Label7.Text =? string.Format("{0:M}",dt);//11月5日
Label8.Text =? string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
Label9.Text =? string.Format("{0:s}",dt);//2005-11-05T14:23:23
Label10.Text = string.Format("{0:t}",dt);//14:23
Label11.Text = string.Format("{0:T}",dt);//14:23:23
Label12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
Label13.Text = string.Format("{0:U}",dt);//2005年11月5日 6:23:23
Label14.Text = string.Format("{0:Y}",dt);//2005年11月
Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23?
Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt);  //yyyymm等可以設(shè)置,比如Label16.Text = string.Format("{0:yyyyMMdd}",dt);

時(shí)間格式化
有時(shí)候我們要對(duì)時(shí)間進(jìn)行轉(zhuǎn)換,達(dá)到不同的顯示效果
默認(rèn)格式為:2005-6-6 14:33:34
假如要換成成200506,06-2005,2005-6-6或更多的該怎么辦呢
我們要用到:DateTime.ToString的方法(String, IFormatProvider)

using System;
using System.Globalization;
String format="D";
DateTime date=DataTime,Now;
Response.Write(date.ToString(format, DateTimeFormatInfo.InvariantInfo));
結(jié)果輸出
Thursday, June 16, 2005

參數(shù)format格式具體用法
格式字符 關(guān)聯(lián)屬性/說(shuō)明
d ShortDatePattern
D LongDatePattern
f 完整日期和時(shí)間(長(zhǎng)日期和短時(shí)間)
F FullDateTimePattern(長(zhǎng)日期和長(zhǎng)時(shí)間)
g 常規(guī)(短日期和短時(shí)間)
G 常規(guī)(短日期和長(zhǎng)時(shí)間)
m、M MonthDayPattern
r、R RFC1123Pattern
s 使用當(dāng)?shù)貢r(shí)間的 SortableDateTimePattern(基于 ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern 用于顯示通用時(shí)間的格式
U 使用通用時(shí)間的完整日期和時(shí)間(長(zhǎng)日期和長(zhǎng)時(shí)間)
y、Y YearMonthPattern

下表列出了可被合并以構(gòu)造自定義模式的模式。這些模式是區(qū)分大小寫(xiě)的;例如,識(shí)別“MM”,但不識(shí)別“mm”。假如自定義模式包含空白字符或用單引號(hào)括起來(lái)的字符,則輸出字符串頁(yè)也將包含這些字符。未定義為格式模式的一部分或未定義為格式字符的字符按其原義復(fù)制。

格式模式 說(shuō)明
d 月中的某一天。一位數(shù)的日期沒(méi)有前導(dǎo)零。
dd 月中的某一天。一位數(shù)的日期有一個(gè)前導(dǎo)零。
ddd 周中某天的縮寫(xiě)名稱,在 AbbreviatedDayNames 中定義。
dddd 周中某天的完整名稱,在 DayNames 中定義。
M 月份數(shù)字。一位數(shù)的月份沒(méi)有前導(dǎo)零。
MM 月份數(shù)字。一位數(shù)的月份有一個(gè)前導(dǎo)零。
MMM 月份的縮寫(xiě)名稱,在 AbbreviatedMonthNames 中定義。
MMMM 月份的完整名稱,在 MonthNames 中定義。
y 不包含紀(jì)元的年份。假如不包含紀(jì)元的年份小于 10,則顯示不具有前導(dǎo)零的年份。
yy 不包含紀(jì)元的年份。假如不包含紀(jì)元的年份小于 10,則顯示具有前導(dǎo)零的年份。
yyyy 包括紀(jì)元的四位數(shù)的年份。
gg 時(shí)期或紀(jì)元。假如要設(shè)置格式的日期不具有關(guān)聯(lián)的時(shí)期或紀(jì)元字符串,則忽略該模式。
h 12 小時(shí)制的小時(shí)。一位數(shù)的小時(shí)數(shù)沒(méi)有前導(dǎo)零。
hh 12 小時(shí)制的小時(shí)。一位數(shù)的小時(shí)數(shù)有前導(dǎo)零。
H 24 小時(shí)制的小時(shí)。一位數(shù)的小時(shí)數(shù)沒(méi)有前導(dǎo)零。
HH 24 小時(shí)制的小時(shí)。一位數(shù)的小時(shí)數(shù)有前導(dǎo)零。
m 分鐘。一位數(shù)的分鐘數(shù)沒(méi)有前導(dǎo)零。
mm 分鐘。一位數(shù)的分鐘數(shù)有一個(gè)前導(dǎo)零。
s 秒。一位數(shù)的秒數(shù)沒(méi)有前導(dǎo)零。
ss 秒。一位數(shù)的秒數(shù)有一個(gè)前導(dǎo)零。
f 秒的小數(shù)精度為一位。其余數(shù)字被截?cái)唷?br />ff 秒的小數(shù)精度為兩位。其余數(shù)字被截?cái)唷?br />fff 秒的小數(shù)精度為三位。其余數(shù)字被截?cái)唷?br />ffff 秒的小數(shù)精度為四位。其余數(shù)字被截?cái)唷?br />fffff 秒的小數(shù)精度為五位。其余數(shù)字被截?cái)唷?br />ffffff 秒的小數(shù)精度為六位。其余數(shù)字被截?cái)唷?br />fffffff 秒的小數(shù)精度為七位。其余數(shù)字被截?cái)唷?br />t 在 AMDesignator 或 PMDesignator 中定義的 AM/PM 指示項(xiàng)的第一個(gè)字符(假如存在)。
tt 在 AMDesignator 或 PMDesignator 中定義的 AM/PM 指示項(xiàng)(假如存在)。
z 時(shí)區(qū)偏移量(“+”或“-”后面僅跟小時(shí))。一位數(shù)的小時(shí)數(shù)沒(méi)有前導(dǎo)零。例如,太平洋標(biāo)準(zhǔn)時(shí)間是“-8”。
zz 時(shí)區(qū)偏移量(“+”或“-”后面僅跟小時(shí))。一位數(shù)的小時(shí)數(shù)有前導(dǎo)零。例如,太平洋標(biāo)準(zhǔn)時(shí)間是“-08”。
zzz 完整時(shí)區(qū)偏移量(“+”或“-”后面跟有小時(shí)和分鐘)。一位數(shù)的小時(shí)數(shù)和分鐘數(shù)有前導(dǎo)零。例如,太平洋標(biāo)準(zhǔn)時(shí)間是“-08:00”。
: 在 TimeSeparator 中定義的默認(rèn)時(shí)間分隔符。
/ 在 DateSeparator 中定義的默認(rèn)日期分隔符。
% c 其中 c 是格式模式(假如單獨(dú)使用)。假如格式模式與原義字符或其他格式模式合并,則可以省略“%”字符。
/ c 其中 c 是任意字符。照原義顯示字符。若要顯示反斜杠字符,請(qǐng)使用“//”。

只有上面第二個(gè)表中列出的格式模式才能用于創(chuàng)建自定義模式;在第一個(gè)表中列出的標(biāo)準(zhǔn)格式字符不能用于創(chuàng)建自定義模式。自定義模式的長(zhǎng)度至少為兩個(gè)字符;例如,

DateTime.ToString( "d") 返回 DateTime 值;“d”是標(biāo)準(zhǔn)短日期模式。
DateTime.ToString( "%d") 返回月中的某天;“%d”是自定義模式。
DateTime.ToString( "d ") 返回后面跟有一個(gè)空白字符的月中的某天;“d”是自定義模式。

比較方便的是,上面的參數(shù)可以隨意組合,并且不會(huì)出錯(cuò),多試試,肯定會(huì)找到你要的時(shí)間格式
如要得到2005年06月 這樣格式的時(shí)間
可以這樣寫(xiě):
date.ToString("yyyy年MM月", DateTimeFormatInfo.InvariantInfo)

=======================================


C#字符串的格式化
一、標(biāo)記
標(biāo)記(tokenizing)是從文本中提取具體內(nèi)容的過(guò)程。
下面的代碼從句子中提取單詞,并把它們輸出到控制臺(tái)。
class mytokenizing
{
   static void Main(string[ ] args)
   {
      string mystring="I like this food,are you?";
      char[] separators={' ',',','?',':','!'};
      int startpos=0;
      int endpos=0;
      do
      {
         endpos=mystring.IndexOfAny(separators,startpos);
         if ( endpos==-1 ) endpos=mystring.Length;
         if ( endpos!=startpos )
              Console.WriteLine(mystring.Substring( startpos,(endpos-startpos)));
         startpos=(endpos+1);
      }while(startpos<mystring.Length);
   }
}
I    <== 輸出
like
this
food
are
you

二、顛倒字符串順序
class myreverse
{
   static void Main(string [] args)
   {
      string mystring="你們好";
      char[] mychars=mystring.ToCharArray( );
      Array.Reverse(mychars);
      Console.WriteLine(mystring);
      Console.WriteLine(mychars);
   }
}
任何繼續(xù)于Array的類都能利用Reverse( )方法為數(shù)組中的元素重新排序。

三、字符串的插入、刪除和替換
示例文件test.txt為字符串的來(lái)源。下面代碼以Unicode格式讀取文本。確保文件保存為讀取時(shí)的格式。例如記事本答應(yīng)將代碼保存為Unicode:
aaaaaaaa,bbbbbbbb,cccccc
dddddddd,eeeeeeee,ffffff
gggggggg,hhhhhhhh,iiiiii
jjjjjjjj,kkkkkkkk,llllll
下面代碼加載數(shù)據(jù)并處理數(shù)據(jù)的測(cè)試工具。測(cè)試結(jié)果發(fā)送給控制臺(tái)。
class myprocessfile
{
   static void Main(string [] args)
   {
      const string myName="test.txt";
      Stream readLine;
      TextWirter writeLine;
      StringBuilder sb;
      readLine=File.OpenRead(myName);
      WriteLine=Console.Out;
      StreamReader readLineSReader=new StreamReader(readLine,Encoding.Unicode);
      readLineSReader.BaseStream.Seek(0,SeekOrigin.Begin);
      while(readLineSReader.Peek()>-1)
      {
         sb=new StringBuilder(readLineSReader.ReadLine());
         //插入字符串操作語(yǔ)句如:sb.Append(",123"); 
         Console.WriteLine(sb.ToString());
      }
   }
}

在結(jié)尾添加一列內(nèi)容:
//displays aaaaaaaa,bbbbbbbb,cccccc,xxxxx
//......
sb.Append(",xxxxx");

第一列可以使用下面的代碼刪除:
//displays bbbbbbbb,cccccc
//......
sb.Remove(0,sb.ToString().IndexOf(',')+1);

替換分隔符:
//aaaaaaaa+bbbbbbbb+cccccc
sb.Replace(',','+');

添加行號(hào)(lineNumber已經(jīng)在前面某處聲明過(guò)為前提):
sb.Insert(0,lineNumber.ToString("000 "));
lineNumber++;

//displays
//000 aaaaaaaa,bbbbbbbb,cccccc
//001 dddddddd,eeeeeeee,ffffff
//002 gggggggg,hhhhhhhh,iiiiii
//003 jjjjjjjj,kkkkkkkk,llllll

字符串操作

--------------------------------------------------------------------------------

1、從字符串中提取子串
StringBuilder 類沒(méi)有支持子串的方法,因此必須用String類來(lái)提取。
string mystring="My name is ynn.";
//Displays "name is ynn."
Console.WriteLine(mystring.Substring( 3 ));
//Displays "ynn"
Console.WriteLine(mystring.Substring( 11,3 ));


2、比較字符串
String 類有四種方法:Compare( )、CompareTo( )、CompareOrdinal( )、Equals( )。
Compare( )方法是CompareTo( )方法的靜態(tài)版本。只要使用“=”運(yùn)算符,就會(huì)調(diào)用Equals( )方法,的以Equals( )方法與“=”是等價(jià)的。CompareOrdinal( )方法對(duì)兩個(gè)字符串比較不考本地語(yǔ)言與文件。
示例:
int result;
bool bresult;
s1="aaaa";
s2="bbbb";
//Compare( )method
//result值為“0”表示等,小于零表示 s1 < s2,大于零表示 s1 > s2
result=String.Compare(s1,s2);
result=s1.CompareTo( s2 );
result=String.CompareOrdinal(s1,s2);
bresult=s1.Equals( s2 );
bresult=String.Equals( s1,s2 );
一個(gè)例外情況是,兩個(gè)字符串都是內(nèi)置的,并相等,靜態(tài)方法要快得多。

3、字符串格式化

3.1 格式化數(shù)字
格式字符    說(shuō)明和關(guān)聯(lián)屬性

--------------------------------------------------------------------------------

c、C       貨幣格式。
d、D       十進(jìn)制格式。
e、E       科學(xué)計(jì)數(shù)(指數(shù))格式。
f、F       固定點(diǎn)格式。
g、G       常規(guī)格式。
n、N       數(shù)字格式。
r、R       往返格式,確保將已轉(zhuǎn)換成字符串的數(shù)字轉(zhuǎn)換回?cái)?shù)字時(shí)具有與原數(shù)字相同的值。
x、X       十六進(jìn)制格式。

--------------------------------------------------------------------------------

double val=Math.PI;
Console.WriteLine(val.ToString( ));  //displays 3.14159265358979
Console.WriteLine(val.ToString("E"));//displays 3.141593E+000
Console.WriteLine(val.ToString("F3");//displays 3.142
int val=65535;
Console.WriteLine(val.ToString("x"));  //displays ffff
Console.WriteLine(val.ToString("X"));  //displays FFFF
Single val=0.123F;
Console.WriteLine(val.ToString("p"));  //displays 12.30 %
Console.WriteLine(val.ToString("p1"));  //displays 12.3 %
默認(rèn)格式化會(huì)在數(shù)字和百分號(hào)之間放入一個(gè)空格。定制方法如下:
其中NumberFormatInfo類是System.Globalization命名空間的一個(gè)成員,因此該命名空間必須導(dǎo)入到程序中。
Single val=0.123F;
object myobj=NumberFormatInfo.CurrentInfo.Clone( ) as NumberFormatInfo;
NumberFormatInfo myformat=myobj as NumberFormatInfo;
myformat.PercentPositivePattern=1;
Console.WriteLine(val.ToString("p",myformat)); //displays 12.30%;
Console.WriteLine(val.ToString("p1",myformat)); //displays 12.3%;
格式化具有很大的靈活性。下面的例子演示一個(gè)沒(méi)有意義的貨幣結(jié)構(gòu):
double val=1234567.89;
int [] groupsize={2,1,3};
object myobj=NumberFormatInfo.CurrentInfo.Clone( );
NumberFormatInfo mycurrency=myobj as NumberFormatInfo;
mycurrency.CurrencySymbol="#"; //符號(hào)
mycurrency.CurrencyDecimalSeparator=":"; //小數(shù)點(diǎn)
mycurrency.CurrencyGroupSeparator="_";  //分隔符
mycurrency.CurrencyGroupSizes=groupsize;
// 輸出 #1_234_5_67:89
Console.WriteLine(val.ToString("C",mycurrency));


3.2 格式化日期
輸出形式取決于用戶計(jì)算機(jī)的文化設(shè)置。
using System;
using System.Globalization;
public class MainClass
{
public static void Main(string[] args) 
{
        DateTime dt = DateTime.Now;
        String[] format = {"d","D","f","F","g","G","m","r","s","t", "T","u", "U","y","dddd, MMMM dd yyyy","ddd, MMM d /"'/"yy","dddd, MMMM dd","M/yy","dd-MM-yy",};
        String date;
      for (int i = 0; i < format.Length; i++)
      {
         date = dt.ToString(format, DateTimeFormatInfo.InvariantInfo);
         Console.WriteLine(String.Concat(format, " :" , date));
      }
     }
}
d :07/11/2004    <=======輸出
D :Sunday, 11 July 2004
f :Sunday, 11 July 2004 10:52
F :Sunday, 11 July 2004 10:52:36
g :07/11/2004 10:52
G :07/11/2004 10:52:36
m :July 11
r :Sun, 11 Jul 2004 10:52:36 GMT
s :2004-07-11T10:52:36
t :10:52
T :10:52:36
u :2004-07-11 10:52:36Z
U :Sunday, 11 July 2004 02:52:36
y :2004 July
dddd, MMMM dd yyyy :Sunday, July 11 2004
ddd, MMM d "'"yy :Sun, Jul 11 '04
dddd, MMMM dd :Sunday, July 11
M/yy :7/04
dd-MM-yy :11-07-04

3.3 格式化枚舉
enum classmen
{
  ynn=1,
  yly=2,
  css=3,
  C++=4
}
獲得枚舉字符串信息如下:
classmen myclassmen=classmen.yly;
Console.WriteLine(myclassmen.ToString( ));   //displays yly
Console.WriteLine(myclassmen.ToString("d")); //displays 2
從系統(tǒng)枚舉中獲得文本人信息如下:
DayOfWeek day=DayOfWeek.Friday;
//displays "Day is Friday"
Console.WriteLine(String.Format("Day is {0:G}",day));
格式化字符串“ G ”把枚舉顯示為一個(gè)字符串。


================================================================

匯總c#.net常用函數(shù)和方法集
1、DateTime 數(shù)字型
System.DateTime currentTime=new System.DateTime();
1.1 取當(dāng)前年月日時(shí)分秒
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)前時(shí)
int 時(shí)=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 取中文日期顯示——年月日時(shí)分
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)前時(shí)分,格式為:14:24
string strT=currentTime.ToString("t");

1.14 取當(dāng)前時(shí)間,格式為:2003-09-23T14:46:48
string strT=currentTime.ToString("s");

1.15 取當(dāng)前時(shí)間,格式為:2003-09-23 14:48:30Z
string strT=currentTime.ToString("u");

1.16 取當(dāng)前時(shí)間,格式為:2003-09-23 14:48
string strT=currentTime.ToString("g");

1.17 取當(dāng)前時(shí)間,格式為:Tue, 23 Sep 2003 14:52:40 GMT
string strT=currentTime.ToString("r");

1.18獲得當(dāng)前時(shí)間 n 天后的日期時(shí)間
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ù)字型
取字串長(zhǎng)度:
如: string str="中國(guó)";
int Len = str.Length ; //Len是自定義變量, str是求測(cè)的字串的變量名

5、System.Text.Encoding.Default.GetBytes(變量)
字碼轉(zhuǎn)換 轉(zhuǎn)為比特碼
如:byte[] bytStr = System.Text.Encoding.Default.GetBytes(str);
然后可得到比特長(zhǎng)度:
len = bytStr.Length;

6、System.Text.StringBuilder("")
字符串相加,(+號(hào)是不是也一樣?)
如:System.Text.StringBuilder sb = new System.Text.StringBuilder("");
sb.Append("中華");
sb.Append("人民");
sb.Append("共和國(guó)");

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、穿過(guò)代理服務(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["變量"];
用超鏈接傳送變量。
如在任一頁(yè)中建超鏈接:<a href=Edit.aspx?fbid=23>點(diǎn)擊</a>
在Edit.aspx頁(yè)中取值:String str=Request.QueryString["fdid"];

12、DOC對(duì)象.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(變量);
向頁(yè)面輸出。

Response.Redirect("URL地址");
跳轉(zhuǎn)到URL指定的頁(yè)面

16、char.IsWhiteSpce(字串變量,位數(shù))——邏輯型
查指定位置是否空字符;
如:
string str="中國(guó) 人民";
Response.Write(char.IsWhiteSpace(str,2)); //結(jié)果為:True, 第一個(gè)字符是0位,2是第三個(gè)字符。

17、char.IsPunctuation(''字符'') --邏輯型
查字符是否是標(biāo)點(diǎn)符號(hào)
如:Response.Write(char.IsPunctuation(''A'')); //返回:False

18、(int)''字符''
把字符轉(zhuǎn)為數(shù)字,查代碼點(diǎn),注重是單引號(hào)。
如:
Response.Write((int)''中''); //結(jié)果為中字的代碼:20013

19、(char)代碼
把數(shù)字轉(zhuǎn)為字符,查代碼代表的字符。
如:
Response.Write((char)22269); //返回“國(guó)”字。

20、 Trim()
清除字串前后空格

21 、字串變量.Replace("子字串","替換為")
字串替換
如:
string str="中國(guó)";
str=str.Replace("國(guó)","央"); //將國(guó)字換為央字
Response.Write(str); //輸出結(jié)果為“中心”

再如:(這個(gè)非常實(shí)用)

string str="這是<script>腳本";
str=str.Replace("<","<font><</font>"); //將左尖括號(hào)替換為<font> 與 < 與 </font> (或換為<,但估計(jì)經(jīng)XML存諸后,再提出仍會(huì)還原)
Response.Write(str); //顯示為:“這是<script>腳本”

假如不替換,<script>將不顯示,假如是一段腳本,將運(yùn)行;而替換后,腳本將不運(yùn)行。
這段代碼的價(jià)值在于:你可以讓一個(gè)文本中的所有HTML標(biāo)簽失效,全部顯示出來(lái),保護(hù)你的具有交互性的站點(diǎn)。
具體實(shí)現(xiàn):將你的表單提交按鈕腳本加上下面代碼:
string strSubmit=label1.Text; //label1是你讓用戶提交數(shù)據(jù)的控件ID。
strSubmit=strSubmit.Replace("<","<font><</font>");
然后保存或輸出strSubmit。
用此方法還可以簡(jiǎn)單實(shí)現(xiàn)UBB代碼。

22、Math.Max(i,j)
取i與j中的最大值
如 int x=Math.Max(5,10); // x將取值 10

23、字串對(duì)比一般都用: if(str1==str2){ } , 但還有別的方法:

(1)、
string str1; str2
//語(yǔ)法: str1.EndsWith(str2); __檢測(cè)字串str1是否以字串str2結(jié)尾,返回布爾值.如:
if(str1.EndsWith(str2)){ Response.Write("字串str1是以"+str2+"結(jié)束的"); }

(2)、
//語(yǔ)法:str1.Equals(str2); __檢測(cè)字串str1是否與字串str2相等,返回布爾值,用法同上.

(3)、
//語(yǔ)法 Equals(str1,str2); __檢測(cè)字串str1是否與字串str2相等,返回布爾值,用法同上.

24、IndexOf() 、LastIndexOf()
查找字串中指定字符或字串首次(最后一次)出現(xiàn)的位置,返回索引值,如:
str1.IndexOf("字"); //查找“字”在str1中的索引值(位置)
str1.IndexOf("字串");//查找“字串”的第一個(gè)字符在str1中的索引值(位置)
str1.IndexOf("字串",3,2);//從str1第4個(gè)字符起,查找2個(gè)字符,查找“字串”的第一個(gè)字符在str1中的索引值(位置)

25、Insert()
在字串中指定索引位插入指定字符。如:
str1.Insert(1,"字");在str1的第二個(gè)字符處插入“字”,假如str1="中國(guó)",插入后為“中字國(guó)”;

26、PadLeft()、PadRight()
在字串左(或右)加空格或指定char字符,使字串達(dá)到指定長(zhǎng)度,如:
<%
string str1="中國(guó)人";
str1=str1.PadLeft(10,''1''); //無(wú)第二參數(shù)為加空格
Response.Write(str1); //結(jié)果為“1111111中國(guó)人” , 字串長(zhǎng)為10
%>

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临洮县| 彩票| 长宁县| 辽源市| 宾川县| 塔河县| 鹤岗市| 北川| 儋州市| 冀州市| 新田县| 红桥区| 勃利县| 淮阳县| 道孚县| 行唐县| 葫芦岛市| 新乡县| 建湖县| 江源县| 南昌县| 北辰区| 丰城市| 偏关县| 万宁市| 德钦县| 赞皇县| 当涂县| 陇南市| 武邑县| 论坛| 鹤岗市| 绥阳县| 汉中市| 安西县| 蒲江县| 晴隆县| 元谋县| 垦利县| 浪卡子县| 镇赉县|