回归...
博客园
首页
社区
订阅
管理
随笔-43 评论-206 文章-0 trackbacks-11
查找匹配度最高的字符串 -- 代码
定义的一个主体类:
public
delegate
decimal
CompareCondition(String value);
public
class
StringCompare
{
private
List
<
IntraString
>
intraList
=
new
List
<
IntraString
>
();
public
StringCompare(List
<
String
>
strlist)
{
for
(
int
i
=
0
; i
<
strlist.Count; i
++
)
{
intraList.Add(
new
IntraString(strlist[i]));
}
}
public
List
<
IntraString
>
Compare(CompareCondition condition)
{
for
(
int
i
=
0
; i
<
intraList.Count; i
++
)
{
intraList[i].Compare(condition);
}
intraList.Sort(
delegate
(IntraString a, IntraString b)
{
if
(a.CompareResult
>
b.CompareResult)
{
return
1
;
}
else
if
(a.CompareResult
==
b.CompareResult)
{
return
0
;
}
else
{
return
-
1
;
}
}
);
return
intraList;
}
}
public
class
IntraString
{
private
String value;
public
IntraString(String value)
{
this
.value
=
value;
}
public
String Value
{
get
{
return
value; }
set
{
this
.value
=
value; }
}
private
decimal
compareResult;
public
decimal
CompareResult
{
get
{
return
compareResult; }
set
{ compareResult
=
value; }
}
public
void
Compare(CompareCondition condition)
{
CompareResult
=
condition(Value);
}
}
比较规则的实现:
/**/
///
<summary>
///
定义比较的条件
///
</summary>
///
<param name="value"></param>
///
<returns></returns>
private
static
decimal
Compare(String value)
{
string
str
=
Guid.NewGuid().ToString().Replace(
"
-
"
,
""
);
int
MaxLength
=
str.Length;
if
(str.Length
<
value.Length)
{
MaxLength
=
value.Length;
}
bool
find
=
false
;
decimal
sameCount
=
0
;
for
(
int
i
=
0
; i
<
value.Length; i
++
)
{
string
newStr
=
value.Substring(i, value.Length
-
i);
for
(
int
j
=
newStr.Length
-
1
; j
>=
0
; j
--
)
{
if
(str.IndexOf(newStr.Substring(
0
,j
+
1
))
>=
0
)
{
newStr
=
newStr.Substring(
0
, j
+
1
);
find
=
true
;
break
;
}
}
if
(find)
{
sameCount
=
newStr.Length;
break
;
}
}
return
sameCount
/
MaxLength;
}
绿色通道:
好文要顶
关注我
收藏该文
与我联系
posted on 2006-12-06 20:43
tablefor2
阅读(948)
评论(1)
编辑
收藏
评论:
634421
#1楼
2007-01-30 11:37
|
skad[未注册用户]
高手真多!!
反醒一下!!!
回复
引用
注册用户登录后才能发表评论,请
登录
或
注册
,
返回博客园首页
。
首页
博问
闪存
新闻
园子
招聘
知识库
最新IT新闻
:
·
纽曼使用42套盗版微软软件被判侵权
·
Adobe承诺5至10年内继续投资Flash技术
·
为什么开发者做UI很烂?
·
“Do not track” 协议,解决网络隐私问题的一个尝试
·
苹果股东大会概述:没新闻/没抱怨/没派息
»
更多新闻...
最新知识库文章
:
·
编程的艺术:漂亮的代码和漂亮的软件
·
GIT分支管理是一门艺术
·
编程:是一门艺术
·
编程是一门艺术吗?
·
对Java初学者的忠告
»
更多知识库文章...
China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
搜索
随笔分类
.Net FrameWork(17)
.Net FrameWork 3.0(12)
Asp.Net(9)
C#(4)
Other(8)
RIA(2)
Script(1)
Links
Wkcow's Space
牛人的Blog,主要关注互联网,搜索
将进酒,杯莫停
浮躁的人,浮躁的Internet
评论排行榜