Tag: string
常用正则表达式
by 赵马 on Sep.20, 2009, under dotNET
“^\d+$” //非负整数(正整数 + 0)
“^[0-9]*[1-9][0-9]*$” //正整数
“^((-\d+)|(0+))$” //非正整数(负整数 + 0)
“^-[0-9]*[1-9][0-9]*$” //负整数
“^-?\d+$” //整数
“^\d+(\.\d+)?$” //非负浮点数(正浮点数 + 0)
“^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$” //正浮点数
“^((-\d+(\.\d+)?)|(0+(\.0+)?))$” //非正浮点数(负浮点数 + 0)
“^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$” //负浮点数
“^(-?\d+)(\.\d+)?$” //浮点数
“^[A-Za-z]+$” //由26个英文字母组成的字符串
“^[A-Z]+$” //由26个英文字母的大写组成的字符串
“^[a-z]+$” //由26个英文字母的小写组成的字符串
“^[A-Za-z0-9]+$” //由数字和26个英文字母组成的字符串
“^\w+$” //由数字、26个英文字母或者下划线组成的字符串
“^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$” //email地址
“^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$” //url
/^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/ // 年-月-日
/^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/ // 月/日/年
“^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$” //Emil
“(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?” //电话号码
“^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$” //IP地址
YYYY-MM-DD基本上把闰年和2月等的情况都考虑进去了
^((((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-))$
string.ToString()格式
by 赵马 on Sep.18, 2009, under dotNET
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); //FA
Console.Write(“{0:X}”, 0xffff); //FFFF
C |
货币 |
2.5.ToString(“C”) |
¥2.50 |
D |
十进制数 |
25.ToString(“D5″) |
00025 |
E |
科学型 |
25000.ToString(“E”) |
2.500000E+005 |
F |
固定点 |
25.ToString(“F2″) |
25.00 |
G |
常规 |
2.5.ToString(“G”) |
2.5 |
N |
数字 |
2500000.ToString(“N”) |
2,500,000.00 |
X |
十六进制 |
255.ToString(“X”) |
FF |
formatCode 是可选的格式化代码字符串。(详细内容请搜索“格式化字符串”查看)
必须用“{”和“}”将格式与其他字符分开。如果恰好在格式中也要使用大括号,可以用连续的两个大括号表示一个大括号,即: “{{”或者“}}”。
常用格式举例:
(1) int i=12345;
this.textBox1.Text=i.ToString();
//结果 12345(this指当前对象,或叫当前类的实例)
this.textBox2.Text=i.ToString(“d8″);
//结果 00012345
(2) int i=123;
double j=123.45;
string s1=string.Format(“the value is {0,7:d}”,i);
string s2=string.Format(“the value is {0,7:f3}”,j);
this.textBox1.Text=s1 ;
//结果 the value is 123
this.textBox2.Text=s2;
//结果 the value is 123.450
(3)double i=12345.6789;
this.textBox1.Text=i.ToString(“f2″); //结果 12345.68
this.textBox2.Text=i.ToString(“f6″);
//结果 12345.678900
(4)double i=12345.6789;
this.textBox1.Text=i.ToString(“n”); //结果 12,345.68
this.textBox2.Text=i.ToString(“n4”); //结果 12,345.6789
(5)double i=0.126;
string s=string.Format(“the value is {0:p}”,i);
this.textBox1.Text=i.ToString(“p”); //结果 12.6%
this.textBox2.Text=s; //结果 the value is 12.6%
(6) DateTime dt =new DateTime(2003,5,25);
this.textBox1.Text=dt.ToString(“yy.M.d”);
//结果 03.5.25
this.textBox2.Text=dt.ToString(“yyyy年M月”);
//结果 2003年5月
Convert.ToDateTime(“2005/12/22 22:22:22″).ToString(“yyyy/MM/dd HH:mm:ss”)
“2005/12/22 22:22:22″
(7) int i=123;
double j=123.45;
string s=string.Format(“i:{0,-7},j:{1,7}”,i,j);
//-7表示左对齐,占7位
this.textBox1.Text=s ;
//结果i:123 ,j: 123.45