【问题标题】:How to find word differences between two strings?如何找到两个字符串之间的单词差异?
【发布时间】:2014-05-20 07:11:06
【问题描述】:

我陷入了一个问题。

我想使用 SQL Server 或任何第三方工具从两个字符串中区分单词。

例如:

First String  ==>   "This is for test."
Second String ==>   "This is a for test."

Output        ==>   "a" from second string 

或:

First String  ==>   "abc This is for test."
Second String ==>   "This is a for test. This is for test."

Output        ==>   "abc" in first string and "a" from second string 

【问题讨论】:

标签: asp.net sql-server c#-4.0


【解决方案1】:

Sql-server 可能不是最好的工具,但你可以使用这样的脚本,它将空格处的所有单词拆分,并获取列表单词之间的差异。所以 1 文本中不存在于其他文本中的单词将包含在输出中,最后脚本将连接单词:

declare @str1 varchar(2000) = 'abc This is for test. dfg'
declare @str2 varchar(2000) = 'This is a for test. This is for test.'

declare @output1 varchar(2000)
declare @output2 varchar(2000)

SELECT @output1 = case when grp = 1 then coalesce(@output1+ ' ' + col, col)  else @output1 end,
       @output2 = case when grp = 2 then coalesce(@output2+ ' ' + col, col)  else @output2 end
FROM
(values(@str1, @str2, 1),(@str2, @str1, 2)) x(str1, str2, grp)
CROSS APPLY
(
SELECT t.c.value('.', 'VARCHAR(2000)') col
     FROM (
         SELECT x = CAST('<t>' + 
               REPLACE(str1, ' ', '</t><t>') + '</t>' AS XML)
     ) a
     CROSS APPLY x.nodes('/t') t(c)
EXCEPT 
SELECT t.c.value('.', 'VARCHAR(2000)')
     FROM (
         SELECT x = CAST('<t>' + 
               REPLACE(str2, ' ', '</t><t>') + '</t>' AS XML)
     ) a
     CROSS APPLY x.nodes('/t') t(c)
) y
SELECT @output1 FirstString, @output2 SecondString

结果:

FirstString  SecondString
abc dfg      a

【讨论】:

  • 感谢您的回答。
猜你喜欢
  • 1970-01-01
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-10
  • 1970-01-01
相关资源
最近更新 更多