从字符串在传统的ASP
在传统的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帮助将不胜感激。 感谢
要删除字符串列表的副本,最好的选择海事组织使用Dictionary对象。您可以使用此功能的短做一个给定的字符串数组的任务:
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中为变量名使用前缀是很常见的做法。
如果它是一个有序列表,可以考虑使用一个变量,最后一个值:
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
谢谢。但解决方案并没有为我工作。 – user1783170
@ user1783170你能解释一下没有用的东西吗?你最后还有'response.write x'吗? – Dijkgraaf
绝对是一个清晰而且写得很好的回应。获得我的投票。 –