2008年5月26日 星期一

C# 運算子

下表根據運算子所執行的運算類型,將運算子分為幾類。每個分類都是依照優先順序列出。

主要
 x.y、f(x)、a[x]、x++、x--、new、typeof、checked、unchecked

一元 +、-、!、~、++x、--x、(T)x

算術 (乘法類) *, /, %

算術 (加法類) +, -

移位 <<, >>

關係和型別測試 <, >, <=, >=, is, as

相等 ==, !=

邏輯 (依照優先順序排列) &, ^, |

條件 (依照優先順序排列) &&, ||, ?:

設定 =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=

C# 使用字串

逸出字元

像是 "\n" (新增一行) 和 "\t" (定位鍵) 的逸出字元可包含在字串中。

程式碼行:
string hello = "Hello\nWorld!";

產出
Hello

World!

============================
如果要包含反向斜線,在反向斜線之前必須有另一個反向斜線。
下列字串:
string filePath = "\\\\My Documents\\";

產出
\\My Documents\

====================================
@ 符號

@ 符號會告訴字串建構函式忽略逸出字元和分行符號。
下列兩個字串是完全相同的:
string p1 = "\\\\My Documents\\My Files\\";
string p2 = @"\\My Documents\My Files\";

================================================
要在字串中搜尋字串,請使用 IndexOf()。如果找不到搜尋字串,IndexOf() 會傳回 -1,否則,會傳回第一個發生位置之以零起始的索引。

string s9 = "Battle of Hastings, 1066";

System.Console.WriteLine(s9.IndexOf("Hastings")); // outputs 10
System.Console.WriteLine(s9.IndexOf("1967")); // outputs -1

==================================================
將字串分隔為子字串

將字串分隔為子字串 (例如,將句子分隔為個別文字) 是常見的程式設計工作。Split() 方法使用分隔符號 (例如,空白字元) 的 char 陣列,並且傳回子字串陣列。您可以用 foreach 存取這個陣列,如下所示:

char[] delimit = new char[] { ' ' };
string s10 = "The cat sat on the mat.";
foreach (string substr in s10.Split(delimit))
{
System.Console.WriteLine(substr);
}

產出
The

cat

sat

on

the

mat.


=============================
http://msdn.microsoft.com/zh-tw/library/ms228362(VS.80).aspx

C# → Main() 傳回值

Main 方法可以屬於型別 void:

static void Main()
{
//...
}

它也可以傳回 int:

static int Main()
{
//...
return 0;
}

========MainReturnValue.cs===================================================

class MainReturnValue
{
static int Main()
{
//...
return 0;
}
}

===============text.bat=============================================

rem test.bat
@echo off
MainReturnValue
@if "%ERRORLEVEL%" == "0" goto good

:fail
echo Execution Failed
echo return value = %ERRORLEVEL%
goto end

:good
echo Execution Succeded
echo return value = %ERRORLEVEL%
goto end

:end

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

http://msdn.microsoft.com/zh-tw/library/0fwzzxz2(VS.80).aspx

2008年5月23日 星期五

ASP.NET AJAX 元件功能

http://www.player.idv.tw/prog/index.php?title=ASP.NET_AJAX&variant=zh-hans

PostgreSQL

PostgreSQL



http://twpug.net/docs/postgresql-doc-8.0-zh_TW/

2008年5月22日 星期四

M$N 去廣告

「hosts」檔案裡的最下面加入下面兩行字:

127.0.0.1 rad.msn.com
127.0.0.1 rad.live.com


Win 98/ME 的修改位置:「C:\WINDOWS」資料夾中的「hosts」檔案。


Windows 2000 : 「C:\WINNT\SYSTEM32\DRIVERS\ETC」資料夾中的「hosts」檔案。


Windows XP:「C:\WINDOWS\system32\drivers\etc」 資料夾中的「hosts」檔案。


Windows Vista:「C:\Windows\System32\drivers\etc」資料夾中的「hosts」檔案。

2008年5月21日 星期三

C# 中的進制轉換

int iNum = 253;
string strResult;

strResult = Convert.ToString(iNum, 2);
// 結果:11111101

strResult = Convert.ToString(iNum, 8);
// 結果:375

strResult = Convert.ToString(iNum, 10);
// 結果:253

strResult = Convert.ToString(iNum, 16);
// 結果:fd

C#字串輸出格式控制

C#的String.Format舉例

stringstr1 =string.Format("{0:N1}",56789); //result: 56,789.0
stringstr2 =string.Format("{0:N2}",56789); //result: 56,789.00
stringstr3 =string.Format("{0:N3}",56789); //result: 56,789.000
stringstr8 =string.Format("{0:F1}",56789); //result: 56789.0
stringstr9 =string.Format("{0:F2}",56789); //result: 56789.00
stringstr11 =(56789 / 100.0).ToString("#.##"); //result: 567.89
stringstr12 =(56789 / 100).ToString("#.##"); //result: 567

C 或 c
貨幣
Console.Write("{0:C}", 2.5); //$2.50
Console.Write("{0:C}", -2.5); //($2.50)

D 或 d
十進位數字
Console.Write("{0:D5}", 25); //00025

E 或 e
科學型
Console.Write("{0:E}", 250000); //2.500000E+005

F 或 f
固定點
Console.Write("{0:F2}", 25); //25.00
Console.Write("{0:F0}", 25); //25

G 或 g
常規
Console.Write("{0:G}", 2.5); //2.5

N 或 n
數字
Console.Write("{0:N}", 2500000); //2,500,000.00

X 或 x
十六進位
Console.Write("{0:X}", 250);

/******************************************************************************/
ASP.NET設置資料格式與String.Format使用總結(引)
{0:d} YY-MM-DD

{0:p} 百分比00.00%

{0:N2} 12.68

{0:N0} 13

{0:c2} $12.68

{0:d} 3/23/2003

{0:T} 12:00:00 AM

{0:男;;女}

DataGrid-資料格式設置運算式

資料格式設置運算式

.NET Framework 格式設置運算式,它在資料顯示在列中之前先應用於資料。此運算式由可選靜態文本和用以下格式表示的格式說明符組成:

{0:format specifier}

零 是參數索引,它指示列中要格式化的資料元素;因此,通常用零來指示第一個(且唯一的)元素。format specifier 前面有一個冒號 (:),它由一個或多個字母組成,指示如何格式化資料。可以使用的格式說明符取決於要格式化的資料類型:日期、數位或其他類型。下表顯示了不同資料類型的 格式設置運算式的示例。有關格式設置運算式的更多資訊,請參見格式化類型。

格式設置運算式


應用於此資料類型


說明


Price: {0:C}


numeric/decimal


顯示“Price:”,後跟以貨幣格式表示的數字。貨幣格式取決於通過 Page 指令或 Web.config 檔中的區域性屬性指定的區域性設置。


{0:D4}


integer(不能和小數一起使用。)


在由零填充的四個字元寬的欄位中顯示整數。


{0:N2}%


numeric


顯示精確到小數點後兩位元的數字,後跟“%”。


{0:000.0}


numeric/decimal


四捨五入到小數點後一位元的數字。不到三位元的數字用零填充。


{0:D}


date/datetime


長日期格式(“Thursday, August 06, 1996”)。日期格式取決於頁或 Web.config 檔的區域性設置。


{0:d}


date/datetime


短日期格式(“12/31/99”)。


{0:yy-MM-dd}


date/datetime


用數字的年-月-日表示的日期(96-08-06)。

唯讀

當此列處於編輯模式時,該列中的資料是否顯示在可編輯的控制項中。

2006-02-22 | asp.net資料格式的Format-- DataFormatString

我 們在呈現資料的時候,不要將未經修飾過的資料呈現給使用者。例如金額一萬元,如果我們直接顯示「10000」,可能會導致使用者看成一千或十萬,造成使用 者閱讀資料上的困擾。若我們將一萬元潤飾後輸出為「NT$10,000」,不但讓使比較好閱讀,也會讓使用者減少犯錯的機會。
下列畫面為潤飾過的結果:
上述資料除了將DataGrid Web 控制項以顏色來區隔記錄外,最主要將日期、單價以及小計這三個計欄位的資料修飾的更容易閱讀。要修飾欄位的輸出,只要設定欄位的DataFormatString 屬性即可;其使用語法如下:

DataFormatString="{0:格式字串}"

我 們知道在DataFormatString 中的 {0} 表示資料本身,而在冒號後面的格式字串代表所們希望資料顯示的格式;另外在指定的格式符號後可以指定小數所要顯示的位元數。例如原來的資料為 「12.34」,若格式設定為 {0:N1},則輸出為「12.3」。其常用的數值格式如下表所示:

格式字串 資料 結果
"{0:C}" 12345.6789 $12,345.68
"{0:C}" -12345.6789 ($12,345.68)
"{0:D}" 12345 12345
"{0:D8}" 12345 00012345
"{0:E}" 12345.6789 1234568E+004
"{0:E10}" 12345.6789 1.2345678900E+004
"{0:F}" 12345.6789 12345.68
"{0:F0}" 12345.6789 12346
"{0:G}" 12345.6789 12345.6789
"{0:G7}" 123456789 1.234568E8
"{0:N}" 12345.6789 12,345.68
"{0:N4}" 123456789 123,456,789.0000
"Total: {0:C}" 12345.6789 Total: $12345.68

其常用的日期格式如下表所示:

格式 說明 輸出格式
d 精簡日期格式 MM/dd/yyyy
D 詳細日期格式 dddd, MMMM dd, yyyy
f 完整格式 (long date + short time) dddd, MMMM dd, yyyy HH:mm
F
完整日期時間格式
(long date + long time)
dddd, MMMM dd, yyyy HH:mm:ss
g 一般格式 (short date + short time) MM/dd/yyyy HH:mm
G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss
m,M 月日格式 MMMM dd
s 適中日期時間格式 yyyy-MM-dd HH:mm:ss
t 精簡時間格式 HH:mm
T 詳細時間格式 HH:mm:ss

string.format格式結果

String.Format

(C) Currency: . . . . . . . . ($123.00)

(D) Decimal:. . . . . . . . . -123

(E) Scientific: . . . . . . . -1.234500E+002

(F) Fixed point:. . . . . . . -123.45

(G) General:. . . . . . . . . -123

(N) Number: . . . . . . . . . -123.00

(P) Percent:. . . . . . . . . -12,345.00 %

(R) Round-trip: . . . . . . . -123.45

(X) Hexadecimal:. . . . . . . FFFFFF85

(d) Short date: . . . . . . . 6/26/2004

(D) Long date:. . . . . . . . Saturday, June 26, 2004

(t) Short time: . . . . . . . 8:11 PM

(T) Long time:. . . . . . . . 8:11:04 PM

(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM

(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM

(g) General date/short time:. 6/26/2004 8:11 PM

(G) General date/long time: . 6/26/2004 8:11:04 PM

(M) Month:. . . . . . . . . . June 26

(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT

(s) Sortable: . . . . . . . . 2004-06-26T20:11:04

(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)

(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM

(Y) Year: . . . . . . . . . . June, 2004

(G) General:. . . . . . . . . Green

(F) Flags:. . . . . . . . . . Green (flags or integer)

(D) Decimal number: . . . . . 3

(X) Hexadecimal:. . . . . . . 00000003

說明:
String.Format
將指定的 String 中的每個格式項替換為相應物件的值的文本等效項。

例子:

int iVisit = 100;
string szName = "Jackfled";
Response.Write(String.Format("您的帳號是:{0} 。訪問了 {1} 次.", szName, iVisit));

C# 存取修飾詞

存取修飾詞是用於指定成員或型別之宣告存取範圍的關鍵字。
`

*

public
*

protected
*

internal
*

private



下列五種存取範圍層級可以使用存取修飾詞來指定:

public:存取沒有限制。

protected:存取只限於包含類別或衍生自包含類別的型別。

internal
:存取只限於目前的組件。

protected internal
:存取只限於目前的組件或衍生自包含類別的型別。

private:存取只限於包含型別(類別)。


宣告存取範圍 意義

public 存取沒有限制。

protected 存取只限於包含的類別或衍生自包含類別的型別。

internal 存取只限於目前的組件。

protected internal
存取只限於目前的組件或衍生自包含類別的型別。

private 存取只限於包含類別。



http://msdn.microsoft.com/zh-tw/library/wxh6fsc7%28VS.80%29.aspx

C# 使Function傳回多

C# 2.0

List<T>
可以依照索引存取的強型別物件清單。提供搜尋、排序和管理清單的方法。


Iterator
Iterator 是 C# 2.0 中的新功能。Iterator 是一種方法、get 存取子或運算子,它可讓您支援類別或結構中的 foreach 反覆運算,而不需要實行整個 IEnumerable 介面。相反地,您只要提供 Iterator,它會只往返於類別中的資料結構。當編譯器偵測到您的 Iterator 時,它會自動產生 IEnumerable 或 IEnumerable 介面的 Current、MoveNext 和 Dispose 方法。

Iterator 概觀
Iterator 是程式碼區段,會傳回相同型別之按順序排列的值。

Iterator 可以當做方法主體、運算子或 get 存取子使用。

Iterator 程式碼會使用 yield return 陳述式輪流傳回各元素。yield break 則會結束反覆運算。如需詳細資訊,請參閱 yield。

可在類別上實作多個 Iterator。每個 Iterator 必須像任何類別成員一樣擁有唯一名稱,且可以由 foreach 陳述式中的用戶端程式碼叫用,如下所示:foreach(int x in SampleClass.Iterator2){}。

Iterator 的傳回型別必須是 IEnumerable、IEnumerator、IEnumerable 或 IEnumerator。

yield 關鍵字可用來指定傳回的值。當到達 yield return 陳述式時,便會儲存目前的位置。此 Iterator 下一次被呼叫時,就會從這個位置重新執行。

Iterator 特別適合與集合類別搭配使用,因為能夠提供逐一查看像是二元樹等複雜資料結構的方法。

2008年5月20日 星期二

Distributed Transaction Coordinator

SQL Transaction


類別
System.Transactions



服務

需開啟 DTC

Distributed Transaction Coordinator

協調跨越多個資源管理員的交易,比如資料庫、訊息佇列及檔案系統。如果此服務被停止,這些交易將不會發生。如果服務被停用,任何明顯

依存它的服務將無法啟動。

2008年5月19日 星期一

Roadmap of Programming and Development 程式開發的學習藍圖

Roadmap of Programming and Development
程式開發的學習藍圖

作者:朱明中

http://www.microsoft.com/taiwan/msdn/columns/jhu_ming_jhong/rop.htm

BasePage

public class BasePage : System.Web.UI.Page
{
public BasePage():base()
{
}
protected override void OnPreLoad(EventArgs e)
{
base.OnPreLoad(e);
if (this.ValidatePermission() == false)
{
throw new Exception("Not Auth");
}
}
============================================================================
protected virtual bool ValidatePermission()
{
return false;
}
}


public partial class _Default : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
this.DataBind();
}
protected override bool ValidatePermission()
{
If( User.Name == "維克大隊長")
{
return true;
}
else
{
return false;
}
}
}

2008年5月9日 星期五

C#與VB類別共用

專案裡的App_Code,如果放.cs 和.vb的類別

在Web.config裡面加入下面語法







完整Web.config如下:

















特殊符號

㊊328A ㊋328B ㊌328C ㊍328D ㊎328E ㊏328F
㊐3290 ㊑3291 ㊒3292 ㊓3293 ㊔3294 ㊕3295 ㊖3296 ㊗3297
㊢32A2 ㊣32A3 ㊤32A4 ㊥32A5 ㊦32A6 ㊧32A7 ㊨32A8 ㊩32A9
㊫32AB ㊬32AC ㊭32AD ㊮32AE ㊯32AF
♀2640 ♁2641 ♂2642 ♃2643 ♄2644 ♅2645 ♆2646 ♇2647 ☀2600 ☁2601 ☂2602 ☃2603 ☄2604 ★2605 ☆2606 ☇2607
♈2648 ♉2649 ♊264A ♋264B ♌264C ♍264D
♎264E ♏264F ♐2650 ♑2651 ♒2652 ♓2653
♔2654 ♕2655 ♖2656 ♗2657 ♘2658 ♙2659
♠2660 ♡2661 ♢2662 ♣2663 ♤2664 ♥2665 ♦2666♧2667 ♨2668
♩2669 ♪266A ♫266B ♬266C ♭266D ♮266E♯266F
✁2701 ✂2702✃2703 ✄2704 ✆2706 ✈2708
✉2709 ✐2710 ✑2711✒2712 ✓2713 ✔2714 ✕2715 ✖2716
✗2717✘2718 ✙2719 ✠2720 ✡2721 ✢2722 ✣2723✤2724
✥2725 ✦2726 ✧2727 ✩2729 ✰2730✱2731 ✲2732 ✳2733☈2608 ☉2609 ☊260A ☋260B ☌260C ☍260D ☎260E ☏260F
☑2611 ☒2612 ☓2613
☚261A ☛261B ☜261C ☝261D ☞261E ☟261F
☠2620 ☡2621 ☢2622 ☣2623 ☤2624 ☥2625 ☦2626 ☧2627
☨2628 ☩2629 ☪262A ☫262B ☬262C ☭262D ☮252E ☯262F
☰2630 ☱2631☲2632 ☳2633☴2634 ☵2635 ☶2636☷2637
✴2734 ✵2735 ✶2736✷2737 ✸2738 ✹2739 ✿273F
❀2740 ❁2741 ❂2742❃2743 ❄2744 ❅2745 ❆2746 ❇2747
☸2638 ☹2639 ☺263A ☻263B ☼263C ☽263D ☾263E ☿263F
❡2761❢2762 ❣2763 ❤2764 ❥2765 ❦2766 ❧2767
ⓐ24D0 ⓑ24D1 ⓒ24D2 ⓓ24D3 ⓔ24D4 ⓕ24D5 ⓖ24D6
ⓗ24D7 ⓘ24D8 ⓙ24D9 ⓚ24DA ⓛ24DB ⓜ24DC ⓝ24DD
ⓞ24DE ⓟ24DF ⓠ24E0 ⓡ24E1 ⓡ24E2 ⓣ24E3 ⓤ24E4
❈2748❉2749 ❊274A ❋274B
❍274D ❏274F ❐2750 ❑2751 ❒2752

清除IE記錄資訊

# 清除 IE7 temporary Internet files 紀錄
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8

# 清除 IE7 cookies 紀錄
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2

# 清除 IE7 瀏覽紀錄
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1

# 清除 IE7 表單資料
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16

# 清除 IE7 已記憶的表單ID與密碼記錄
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32

# 清除 IE7 全部的記錄
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255

# 清除 IE7 附加元件所產生的資訊記錄
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 4351

2008年4月10日 星期四

C# Active Directory

在Microsoft .NET Framework 中,ADSI是獨立在一個命名空間 System.DirectoryService,預設是沒被加入參考
它提供二個類別 一個是DirectoryEntry,另一個是 DirectorySearcher , 二個適用範圍不同
DirectoryEntry用來存取物件資料
DirectorySearcher 負責AD的搜尋


如果你要用System.DirectoryService存取AD,要準備三件事

一、將System.DirectoryService.dll加入參考
二、準備好可登入網域 且有可存取指定資源權限的使用考帳號碼含名稱跟密碼
三、 將想要查詢的物件階層,組合成LDAP查詢字串 (LDAP查詢字串是存取Active Directory物件的核心,每一個在AD中的物件,
都會有一個固定的唯一名稱,這個名稱叫做Distinguished Names(DN), DirectoryEntry會依LDAP所給的DN來搜尋AD,若LDAP指示的DN
找不到資料,就會傳回伺服器中無此物件的訊息。

LDAP 查詢字串的結構為

LDAP://[distinguished name]
LDAP://cn=[common name], ou=[Organization Unit Name], dc =[Doma

例如,要查詢 Acme.com的DC中的Administrator使用者時,若只有單一層的Domain 那LDAP的字串會簡單多

LDAP://cn=Administrator,cn=Users, dc=acme,dc=com

如何用AD做使用者驗證?

首先 先要由表單取得使用者輸入的名稱與密碼

並把它綁到DirectoryEntry物 件中

若要在驗證時進一步取到使用者的資訊 像uid或 sid


那可和DirectorySearcher 做整合然後啟動搜尋

若找不到使用者的物件 則可傳回錯誤

可用SearchResult.GetDirectoryEntry()來取得使用者的AD物件



DirectorySearcher searcher = new DirectorySearcher ( new DirectoryEntry("LDAP://dc=jcistudio.idv.tw",userName,password)); //設定符合使用者名稱的使用者



searcher.Filler = "(&&objectClass=User)(sAMAccountName=" + userName + "))"; //搜尋使用者

SearchResult result = searcher.Findone();
if( result == null)
Response.Write("User is not exist.") ; //錯誤訊息

2008年4月9日 星期三

XCOPY AutoBackUp

XCOPY 來源資料夾 目的資料夾 /S /D /Y

/S 複製每個目錄及其包含的子目錄。
/D 複製發生變更的檔案。
/Y 所有覆蓋的詢問都回答 YES

EX:
XCOPY "C:\MyDoc" "E:\BackUpFolder" /S /D /Y

=============================================
註解 = rem
EX:
rem Start BackUp Function...

============================================
設定變數 = set
EX:
set A=B

變數 A 的值是 B

使用變數 %變數%
EX:
%A%
============================================

@echo off
cls
echo Start Test Function...
rem 宣告一個變數A等於EE
set A=EE
rem 顯示變數A
echo %A%


rem 宣告一個變數B等於今天日期
set B=%date%
rem 顯示變數B
echo %B%

set year=%date:~0,4%
echo %year%

echo 1234567890
echo ABCDEFGHIJ

set s=ABCDEFGHIJ
echo %s:~0,2%

echo %s:~0,2%
echo %s:~4,4%
echo %s:~4,-4%
echo %s:~,-4%
echo %s:~,-4%
echo %s:~-4%

pause
cls

2008年4月7日 星期一

C#.net常用函數和方法集匯總

C#.net常用函數和方法集匯總


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;
(變數可用中文)

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 取當前時分,格式為:14:24
string strT=currentTime.ToString("t");

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

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

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

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

1.18獲得當前時間 n 天後的日期時間
DateTime newDay = DateTime.Now.AddDays(100);

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["變數"];
用超連接傳送變數。
如在任一頁中建超連接:點擊
在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.Replace("國","央"); //將國字換為央字
Response.Write(str); //輸出結果為「中央」

再如:(這個非常實用)

string str="這是

Search My Blog

分類

Click Me

Chat

雜記

亂七八糟的記憶

funP