如何在VBScript中拆分字符串并批量将颜色值赋值给n系列变量
解决方案:批量动态赋值给n1、n2这类变量
Got it, let's fix this up and get your dynamic variable assignment working properly in VBScript. First, let's correct the initial Split syntax—your current line won't work because the Split function expects a source string and a delimiter, not a list of values. Then we'll use VBScript's Execute statement to create those n1, n2, n3 variables automatically.
Step 1: Correct the String Split
First, properly split your color string into an array:
' Define your source string with colors separated by a delimiter (comma here) colorString = "red,blue,orange,blue" ' Split into an array using the comma as delimiter strArray = Split(colorString, ",")
Step 2: Dynamically Assign Variables
Use a loop with Execute to generate and run the assignment code for each color:
Dim i For i = 0 To UBound(strArray) ' Generate the assignment statement string: n1 = strArray(0), n2 = strArray(1), etc. Dim assignStatement assignStatement = "n" & (i + 1) & " = strArray(" & i & ")" ' Execute the generated statement to create the variable and assign the value Execute assignStatement Next
Step 3: Verify the Assignment
You can test that the variables work by printing them out:
' Print each dynamically created variable For i = 0 To UBound(strArray) Execute "response.write n" & (i + 1) & " & ""<br>""" Next
How This Works
- The
Executefunction takes a string and runs it as VBScript code. This lets us build assignment statements on the fly instead of writing eachn1=red,n2=blueline manually. - We use
i + 1because VBScript arrays are 0-indexed, so the first element becomesn1instead ofn0.
内容的提问来源于stack exchange,提问作者Babar
相关产品推荐
相关产品推荐

