从经典 asp 中的字符串中删除重复值
Remove duplicate values from a string in classic asp
我有以下经典代码 asp
str=Request.Form("txt_str")
"txt_str" 是经典 asp 表单页面中的文本框,我在其中输入以下值:
000-00001
000-00001
000-00001
000-00002
response.write str
因此 str 将为 000-00001 000-00001 000-00001 000-00002
array = split(str,Chr(44))
if str <> "" then
x=empty
for i = 0 to ubound(array)
if array(i) <> "" then
array_2 = split(array(i),chr(13) & chr(10))
for j = 0 to ubound(array_2)
if array_2(j) <> "" then
if x=empty then
x= "'" & array_2(j) & "'"
else
x= x & ",'" & array_2(j) & "'"
end if
end if
next
end if
next
End if
response.write x
因此 x 将返回为“000-00001”、“000-00001”、“000-00001”、“000-00002”
我想从 x 中删除重复值并仅将其显示为:
x = '000-00001','000-00002'
我怎样才能实现 this.Any 在此方面的帮助将不胜感激。
谢谢
如果是有序列表,请考虑使用具有最后一个值的变量:
lastval = ""
array = split(str,Chr(44))
if str <> "" then
x=empty
for i = 0 to ubound(array)
if array(i) <> "" then
array_2 = split(array(i),chr(13) & chr(10))
for j = 0 to ubound(array_2)
if array_2(j) <> "" then
if array_2(j) <> lastval then
lastval = array_2(j)
if x=empty then
x= "'" & array_2(j) & "'"
else
x= x & ",'" & array_2(j) & "'"
end if
end if
end if
next
end if
next
End if
要删除字符串列表的重复项,IMO 的最佳选择是使用字典对象。您可以使用这个短函数对给定的字符串数组执行任务:
Function getUniqueItems(arrItems)
Dim objDict, strItem
Set objDict = Server.CreateObject("Scripting.Dictionary")
For Each strItem in arrItems
objDict.Item(strItem) = 1
Next
getUniqueItems = objDict.Keys
End Function
一个简单的测试:
' -- test output
Dim arrItems, strItem
arrItems = Array("a","b","b","c","c","c","d","e","e","e")
For Each strItem in getUniqueItems(arrItems)
Response.Write "<p>" & strItem & "</p>"
Next
这是您的用例示例:
' -- sample for your use case
Dim strInput, x
strInput = Request.Form("txt_str")
x = "'" & join(getUniqueItems(split(str, Chr(44))), "','") & "'"
顺便说一句,您是否注意到 Array
和 Str
是 VBScript 关键字,因此您可能 运行 遇到使用此类变量名称的问题。因此,我认为在 VBScript 中为变量名使用前缀是常见的做法。
我有以下经典代码 asp
str=Request.Form("txt_str")
"txt_str" 是经典 asp 表单页面中的文本框,我在其中输入以下值:
000-00001
000-00001
000-00001
000-00002
response.write str
因此 str 将为 000-00001 000-00001 000-00001 000-00002
array = split(str,Chr(44))
if str <> "" then
x=empty
for i = 0 to ubound(array)
if array(i) <> "" then
array_2 = split(array(i),chr(13) & chr(10))
for j = 0 to ubound(array_2)
if array_2(j) <> "" then
if x=empty then
x= "'" & array_2(j) & "'"
else
x= x & ",'" & array_2(j) & "'"
end if
end if
next
end if
next
End if
response.write x
因此 x 将返回为“000-00001”、“000-00001”、“000-00001”、“000-00002”
我想从 x 中删除重复值并仅将其显示为:
x = '000-00001','000-00002'
我怎样才能实现 this.Any 在此方面的帮助将不胜感激。 谢谢
如果是有序列表,请考虑使用具有最后一个值的变量:
lastval = ""
array = split(str,Chr(44))
if str <> "" then
x=empty
for i = 0 to ubound(array)
if array(i) <> "" then
array_2 = split(array(i),chr(13) & chr(10))
for j = 0 to ubound(array_2)
if array_2(j) <> "" then
if array_2(j) <> lastval then
lastval = array_2(j)
if x=empty then
x= "'" & array_2(j) & "'"
else
x= x & ",'" & array_2(j) & "'"
end if
end if
end if
next
end if
next
End if
要删除字符串列表的重复项,IMO 的最佳选择是使用字典对象。您可以使用这个短函数对给定的字符串数组执行任务:
Function getUniqueItems(arrItems)
Dim objDict, strItem
Set objDict = Server.CreateObject("Scripting.Dictionary")
For Each strItem in arrItems
objDict.Item(strItem) = 1
Next
getUniqueItems = objDict.Keys
End Function
一个简单的测试:
' -- test output
Dim arrItems, strItem
arrItems = Array("a","b","b","c","c","c","d","e","e","e")
For Each strItem in getUniqueItems(arrItems)
Response.Write "<p>" & strItem & "</p>"
Next
这是您的用例示例:
' -- sample for your use case
Dim strInput, x
strInput = Request.Form("txt_str")
x = "'" & join(getUniqueItems(split(str, Chr(44))), "','") & "'"
顺便说一句,您是否注意到 Array
和 Str
是 VBScript 关键字,因此您可能 运行 遇到使用此类变量名称的问题。因此,我认为在 VBScript 中为变量名使用前缀是常见的做法。