try catch 注意

使用try catch 記得在 I/O(輸入輸出) 及連接資料庫時(關閉資料庫用) 使用 finally

try catch 中 finally 不管有無錯誤都一定會跑的區塊 

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

 

string BidResults = txtBidResults.SelectedItem == null ? "" : txtBidResults.SelectedItem.Value.Trim();

======================================================
Page myPage = this.Page;
myPage.Response.Write("<div id=\"loading\" style=\"position:absolute; width:100%; text-align:center; top:300px;\"><img src=\"../loading/loading25.gif\" border=0></div>");
myPage.Response.Flush();
myPage.Response.Write("<script>document.getElementById('loading').style.display='none';</script>");

==================================================
string strMsg = "";

if (txtdDate.IsEmpty)
{
strMsg += "閱覽日期不可空白<br />" ;
}

if (txtCeDate.IsEmpty)
{
strMsg += "公告日期不可空白<br />";
}

if (txtdDate.IsEmpty)
{
strMsg += "開標日期不可空白<br />";
}

========================================
C++ 換行指令 /r/n , /n

========================================
for (int i = 0; i < MyStr.Length; i++)
{
if (MyStr[i] == '/')
{
return true;
}
}
return


Or

public void(string sql)
{
if (sql.Contains("where")) // 檢查字串是否有"where" ;  ps:如果是sql.Contains('f')   那就查有沒有 f  字元 
return true;
else
return false;
}
==========================================
StringBuilder sb = new StringBuilder("");
sb.Append(" Select * from CPNTR ,CVEN");
sb.Append(" where 1=1");
sb.Append(" and CVEN.Vender_id = CPNTR.VBIDNO");
sb.Append(" and CPNTR.Cntrno = '" + strCntrNo + "'");

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

Substring

String.Substring (Int32) 從此實例檢索子字符串。子字符串從指定的字符位置開始。
String.Substring (Int32, Int32) 從此實例檢索子字符串。子字符串從指定的字符位置開始且具有指定的長度。

Ex: 

string testaa = "1234567aaa";
testaa = testaa.Substring(1, 7);

 

  Ans: testaa = 234567a

 string testaa = "1234567aaa";
 testaa = testaa.Substring(5);

 Ans: testaa = 67aaa

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

IndexOf

 

String.IndexOf 方法 (Char, Int32, Int32)

指定字串在此範例中的第一個匹配項的索引。搜索從指定字符位置開始,並檢查指定數量的字符位置。
String.IndexOf(value, startIndex, count)

參數
value:要查找的 Unicode 字符。
startIndex:搜索起始位置。
count:要檢查的字符位置數。
返回值(Int32)

若說找不到其字串,則回傳-1;

Ex:

string testaa = "1234567aaabcdefghijkLMNOPQ";
: int Count = testaa.IndexOf("1"); 則回傳 0
若: int Count = testaa.IndexOf("7"); 則回傳 6

若:     int Count = testaa.IndexOf("45"); 則回傳 3

若: int Count = testaa.IndexOf("7"); 則回傳 6
若: int Count = testaa.IndexOf("a",5,2); 則回傳 -1  
// 這裡指 第五位置往後找兩個字元,由於找不到"a" 則回傳-1

已上 若 int Count = testaa.IndexOf("a",5,5); 則回傳 7  
// 這裡指 第五位置往後找五個字元找到"a",而a 為第7位置上,則回傳7
 
若:int Count = testaa.IndexOf("aaaa"); 則回傳 -1  
int Count = testaa.IndexOf("aaa"); 則回傳 7

若:int Count = testaa.IndexOf("aa",5); 則回傳 7
若: int Count = testaa.IndexOf("aa",10); 則回傳 -1

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

LastIndexOf

有別於IndexOf 是從尾巴開始找回起始點 但找到的字元/字串 還是回傳此 字元/字串 本身位置
string testaa = "1234567aaabcdefghijkLMNOPQ";

Ex : int Count = testaa.LastIndexOf("a", 5); 則回傳 -1
// 由於從第5字元 往回找 就是從5回找
int Count = testaa.LastIndexOf("a", 10); 則回傳 9 // 
int Count = testaa.LastIndexOf("a", 8); 則回傳 8
int Count = testaa.LastIndexOf("a", 10,3); 則回傳 9 
// 則是從第10位置往回找3個字元 則找到第一個 a 是第九字元
int Count = testaa.LastIndexOf("a", 5,10); 則編譯錯誤; 
// 第5字元回找10個字元但回找仍只有五個字元,已超出範圍外當然編譯錯誤


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

參考網址:http://adisonwu.blogspot.tw/2010/08/converttoint32-int-intparse.html

1. convert.toInt..針對物件,遇到null回傳0,
轉換規則:四捨五入
2. int.parse只能處理字串,遇到null就會失敗,要有檢查機制 盡量放try catch 機制,
轉換規則:無,所以只能用於整數字串
3. (int)僅是轉型,只能針對簡單資料
規則:是無條件捨去




arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JoshS 的頭像
    JoshS

    JoshS的部落格

    JoshS 發表在 痞客邦 留言(0) 人氣()