使用替换某种循环?

时间:2014-08-18 14:29:51

标签: replace vbscript

我有一个带有以下值的变量:

Variable1 = "Apple, Banana, Pineaple, Grape, Coconut"

我想使用Replace获得以下结果:

VariableX = "<span class="text1">Apple</span> <span class="text2">Banana</span> <span class="text1">Pineapple</span> <span class="text2">Grape</span> <span class="text1">Coconut</span>"

因此,第一个值获得text1,第二个获得text2,第三个获得text1,第四个获得text2,依此类推,直到结束。< / p>

1 个答案:

答案 0 :(得分:1)

' Split into an array...
a = Split(Variable1, ",")

' Add each element to a <span> tag...
For i = 0 To UBound(a)
    VariableX = VariableX & "<span class=""text" & i + 1 & """>" & Trim(a(i)) & "</span>"
Next

关于评论的更新:

要在两个值之间切换,您可以将它们放在一个数组中,然后使用Mod来选择其中一个值。

a = Split(Variable1, ",")
strClasses = Array("text-info", "text-warning")

' Add each element to a <span> tag...
For i = 0 To UBound(a)
    VariableX = VariableX & "<span class=""" & strClasses(i Mod 2) & """>" & Trim(a(i)) & "</span>"
Next