本人決定把工作中經(jīng)常用到的C#編程技巧記錄在博客中,以備查閱。
所有的代碼均在 .NET2.0 下測(cè)試通過。引用命名空間如下:
?
1
using
?System;
2
using
?System.Collections;
3
using
?System.Collections.Generic;
4
using
?System.Text;
5
using
?System.Text.RegularExpressions;
?
(1)、刪除List<T>中元素相同的項(xiàng)。
?1
public
?
static
?
void
?GetUniqueList()
?2
{
?3
????
int
[]?a?
=
?
{
2
,
3
,
3
,
4
,
5
,
6
,
6
,
7
,
8
,
9
,
10
,
12
,
12
,
12
,
4
,
18
,
20
,
20
}
;
?4
????List
<
int
>
?lst?
=
?
new
?List
<
int
>
(a);
?5
????List
<
int
>
?retLst?
=
?
new
?List
<
int
>
();
?6
????
foreach
?(
int
?item?
in
?lst)
?7
????
{
?8
????????
if
?(retLst.IndexOf(item)?
==
?
-
1
)
?9
????????
{
10
????????????retLst.Add(item);
11
????????????Console.WriteLine(item);
12
????????}
13
????}
14
}
?
(2)、驗(yàn)證手機(jī)號(hào)
public ? static ? void ?VerifyMobilephone()
{
????Regex?rgxChinaUnicom? = ? new ?Regex( @" ^(130|131|132|133|153|155|156)[0-9]{8}$ " );
????Regex?rgxChinaMobile? = ? new ?Regex( @" ^(134|135|136|137|138|139|150|157|158|159)[0-9]{8}$ " );
????Console.WriteLine( " please?input?Mobilephone?Number: " );
???? string ?a? = ?Console.ReadLine();
???? if (rgxChinaMobile.IsMatch(a))
????{
????Console.WriteLine( " It?is?ChinaMobile?Number! " );
????}
???? else ? if ?(rgxChinaUnicom.IsMatch(a))
????{
????Console.WriteLine( " It?is?ChinaUnicom?Number! " );
????}
???? else
????{
????Console.WriteLine( " you?input?a?Error?Number! " );
????}????
}
?
(3)、在Access中插入一條記錄后得到最新的自動(dòng)編號(hào)
?1
public
?
int
?GetIDInsert(
string
?XSqlString)
?2
{
?3
????????OleDbConnection?con
=
DB.createcon();
?4
????OleDbCommand?cmd?
=
?
new
?OleDbCommand(XSqlString,?con);
?5
????con.Open();
?6
????cmd.ExecuteNonQuery();
?7
????cmd.CommandText?
=
?
"
SELECT?@@IDENTITY
"
;
?8
????
int
?Id?
=
?(
int
)cmd.ExecuteScalar();
?9
????con.Close();
10
????
return
?Id;???????
11
}
?
?(4)、驗(yàn)證字符串中是否包含中文和中文字符
protected ? static ? bool ?HaveChinese( string ?str)
{
???? if ?(Regex.IsMatch(str,? @" [\u4E00-\u9FA5|,。?:;‘’!“”……——、()【】《》¥]+ " ))
????{
???????? return ? true ;
????}
???? else
????{
???????? return ? false ;
????}
}
??
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

