我试图找出一种方法来比较两个字符串并返回它们的“公共”单词,考虑到字符串总是小写的,我想为this..for示例创建一个函数。
str1 = "this is a test"
str2 = "saldkasl test asdasd"
result = stringcompare(str1, str2) 'returns "test"两个字符串之间的常用单词应该是"test“,如果两个字符串有两个或多个通用单词,则函数应该将字符串连接起来。
str1 = "this is another test"
str2 = "another asdsada test asdsa"
result = stringcompare(str1, str2) ' returns "another test"我找到了一个有用的link,它给了我一个想法,但不知怎么的,有些东西真的很缺乏。
我现在在做的一个伪代码是,
**
'1st: separate the words by every space, " ", then store it in an array or list
'2nd: compare each item on the list, if equal then store to variable 'result'**
这样行吗?我认为这是缓慢的,也许有一个人在this..thanks上有一个更好的方法
发布于 2014-01-15 01:50:48
以VS 2013为标准,下面的解决方案比Guffa的平均快20%:
Dim str1 As String = "this is another test"
Dim str2 As String = "another asdsada test asdsa"
Dim result As String = String.Join(" ", str1.Split(" "c).
Intersect(str2.Split(" "c)))每种溶液循环100000次,用StopWatch测定时间,得到结果。
https://stackoverflow.com/questions/21127562
复制相似问题