如何找到两个字符串之间的单词区别是什么? [英] How to find word differences between two strings?
本文介绍了如何找到两个字符串之间的单词区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我被困在一个问题。
我想用SQL Server或任何第三方工具来区分两个字符串的话。
的例如:的
第一个字符串==> 这是一个考验。
第二个字符串==> 这是一个为测试。输出==> 一从第二个字符串
的或的
第一个字符串==> ABC这是一个考验。
第二个字符串==> 这是一个用于测试,这是为测试。输出==>从第二串在第一字符串abc和一个
解决方案
SQL服务器可能不是最好的工具,但你可以使用一个这样的脚本,它将在空间分割的所有文字和走差异化列表中词与词之间。因此,在不存在其他的文本将被包含在输出文本1一个词,最后该脚本将concatinate的话:
声明@ STR1 VARCHAR(2000)='ABC这是考验。 DFG
声明@ STR2 VARCHAR(2000)='这是一个测试。这是为测试。声明@输出1 VARCHAR(2000)
声明@ OUTPUT2 VARCHAR(2000)SELECT @输出1 =情况下,当GRP = 1,则合并(@输出1 +''+ COL,COL)其他@输出1月底,
@输出2 =情况下,当GRP = 2,那么合并(@输出2 +''+ COL,COL)其他@输出2月底
从
(值(@ STR1,@ STR2,1),(@ STR2,@ STR1,2))×(STR1,STR2,GRP)
CROSS APPLY
(
SELECT t.c.value('。','VARCHAR(2000)')山口
FROM(
选择x = CAST('< T>'+
REPLACE(STR1,'','< / T>< T>')+'< / T>' AS XML)
) 一个
CROSS APPLY x.nodes('/ T')T(C)
除
SELECT t.c.value('。','VARCHAR(2000)')
FROM(
选择x = CAST('< T>'+
REPLACE(STR2,'','< / T>< T>')+'< / T>' AS XML)
) 一个
CROSS APPLY x.nodes('/ T')T(C)
)Y
SELECT @输出1 FirstString,@输出2 SecondString
结果:
FirstString SecondString
ABC DFG一个
I am stuck in one problem.
I want to differentiate words from two strings using SQL Server or Any third party tool.
e.g.:
First String ==> "This is for test."
Second String ==> "This is a for test."
Output ==> "a" from second string
or:
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
解决方案
Sql-server may not be the best tool, but you could use a script like this, it will split up all the words at the spaces and take the difference between the list words. So a word in 1 text that doesn't exists in the other text will be included in the output, finally the script will concatinate the words:
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
Result:
FirstString SecondString
abc dfg a
这篇关于如何找到两个字符串之间的单词区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文