2012年8月29日 星期三
string .join
DateTime dt = Convert.ToDateTime(Model.xx);
string format = "yyyy/MM/dd";
string bb= "(" + dt.ToString(format) + ")";
string[] cc= {aa, bb};
string newJoin = string.Join("", cc);
2012年8月28日 星期二
string convert to DateTime C# 兩種做法
string xx= Model.xx;
DateTime dt = Convert.ToDateTime(xx);
string format = "yyyy/MM/dd";
(<%=dt.ToString(format)%>)
FundSizeDate = string.Format("{0}/{1}/{2}", dt.Year, dt.Month, dt.Day);
(<%=String.Format("{0:yyyy/MM/dd}", xx)%>
2012年8月20日 星期一
亂數不重複加排序
<%
ArrayList myArray = new ArrayList();
int number;
System.Random num = new Random();
for (int i = 0; i < 30; i++)
{
do
{
number = num.Next(1, 50);
} while (myArray.IndexOf(number.ToString()) != -1);
myArray.Add(number.ToString());
}
var sortedmyArray = myArray.Cast<string>().OrderBy(item => int.Parse(item));
foreach (var a in sortedmyArray)
{
Response.Write("<LI>" + a);
}
%>
2012年8月16日 星期四
C# Server驗證~!!
public class ServerValidation
{
public struct RegularExp
{
public const string Chinese = @"^[^\u4E00-\u9FA5]$";
public const string Chinese1 = @"^[\u4E00-\u9FA5\uF900-\uFA2D]+$";
public const string Color = "^#[a-fA-F0-9]{6}";
public const string Date = @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";
public const string DateTime = @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$";
public const string Email = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
public const string Float = @"^(-?\d+)(\.\d+)?$";
public const string ImageFormat = @"\.(?i:jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$";
public const string Integer = @"^-?\d+$";
public const string IP = @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$";
public const string Letter = "^[A-Za-z]+$";
public const string LowerLetter = "^[a-z]+$";
public const string MinusFloat = @"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$";
public const string MinusInteger = "^-[0-9]*[1-9][0-9]*$";
public const string Mobile = "^0{0,1}13[0-9]{9}$";
public const string NumbericOrLetterOrChinese = @"^[A-Za-z0-9\u4E00-\u9FA5\uF900-\uFA2D]+$";
public const string Numeric = "^[0-9]+$";
public const string NumericOrLetter = "^[A-Za-z0-9]+$";
public const string NumericOrLetterOrUnderline = @"^\w+$";
public const string PlusFloat = @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
public const string PlusInteger = "^[0-9]*[1-9][0-9]*$";
public const string Telephone = @"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?";
public const string UnMinusFloat = @"^\d+(\.\d+)?$";
public const string UnMinusInteger = @"\d+$";
public const string UnPlusFloat = @"^((-\d+(\.\d+)?)|(0+(\.0+)?))$";
public const string UnPlusInteger = @"^((-\d+)|(0+))$";
public const string UpperLetter = "^[A-Z]+$";
public const string Url = @"^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$";
public const string Guid = @"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$";
}
private static Regex isGuid = new Regex(RegularExp.Guid, RegexOptions.Compiled);
private static Regex isChinese = new Regex(RegularExp.Chinese, RegexOptions.Compiled);
private static Regex isMobile = new Regex(RegularExp.Mobile, RegexOptions.Compiled);
public bool IsGuidValid(string candidate, out Guid marketcontent)
{
marketcontent = Guid.Empty;
try
{
if (isGuid.IsMatch(candidate))
{
marketcontent = new Guid(candidate);
return true;
}
return false;
}
catch(ArgumentException)
{
return false;
}
}
public bool IsChineseValid(string chinese)
{
try
{
if(isChinese.IsMatch(chinese))
return true;
return false;
}
catch(FormatException)
{
return false;
}
}
public bool IsMobileValid(string number)
{
try
{
if(isMobile.IsMatch(number))
return true;
return false;
}
catch (FormatException)
{
return false;
}
}
public bool IsEmailValid(string emailaddress)
{
try
{
MailAddress m = new MailAddress(emailaddress);
return true;
}
catch (FormatException)
{
return false;
}
}
}
2012年8月13日 星期一
(int?)null
d.Cl_Id = Convert.ToInt32(cl_id) == 0 ? (int?)null : Convert.ToInt32(cl_id);
int? abc = (def == "noval" ? (int?)null : Convert.ToInt32(def ));
int? abc = (def == "noval" ? (int?)null : Convert.ToInt32(def ));
2012年8月9日 星期四
2012年8月6日 星期一
Linq Group By
itemList = (from a in xxxxxx
group a by new { a.xxxx, a.xxxx} into g
select new xxxxx
{
Counter = g.Sum(x => x.Counter),
xxx= g.Key.xxxxx,
Unit = g.Key.UnitType
}
).ToList();
2012年8月2日 星期四
取網頁中的原始碼轉成字串
int timeoutSeconds = 1;
// 設定request System.Net.WebRequest request = System.Net.WebRequest.Create(URL);
//設定TimeOut的時間(怕網路速度過慢,資料不完全)
request.Timeout = timeoutSeconds * 1000;
// 從request接收資料
System.Net.WebResponse response = request.GetResponse(); System.IO.Stream streamReceive = response.GetResponseStream(); System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("utf-8"); System.IO.StreamReader streamRead = new System.IO.StreamReader(streamReceive, System.Text.Encoding.Default);
// 取得原始碼
String HTML = streamRead.ReadToEnd();
// 設定request System.Net.WebRequest request = System.Net.WebRequest.Create(URL);
//設定TimeOut的時間(怕網路速度過慢,資料不完全)
request.Timeout = timeoutSeconds * 1000;
// 從request接收資料
System.Net.WebResponse response = request.GetResponse(); System.IO.Stream streamReceive = response.GetResponseStream(); System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("utf-8"); System.IO.StreamReader streamRead = new System.IO.StreamReader(streamReceive, System.Text.Encoding.Default);
// 取得原始碼
String HTML = streamRead.ReadToEnd();
訂閱:
文章 (Atom)