本人决定把工作中经常用到的C#编程技巧记录在博客中,以备查阅。
所有的代码均在 .NET2.0 下测试通过。引用命名空间如下:
1
using
System;
2
using
System.Collections;
3
using
System.Collections.Generic;
4
using
System.Text;
5
using
System.Text.RegularExpressions;
(1)、删除List<T>中元素相同的项。
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)、验证手机号
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中插入一条记录后得到最新的自动编号
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)、验证字符串中是否包含中文和中文字符
protected static bool HaveChinese( string str)
{
if (Regex.IsMatch(str, @" [\u4E00-\u9FA5|,。?:;‘’!“”……——、()【】《》¥]+ " ))
{
return true ;
}
else
{
return false ;
}
}
更多文章、技术交流、商务合作、联系博主
微信扫码或搜索:z360901061
微信扫一扫加我为好友
QQ号联系: 360901061
您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。
【本文对您有帮助就好】元

