Methods of using split to implement array operations under asp
An example of the Split function in ASP
Have you ever encountered that you want to get some values in a string but you can't start? Do you think the way to write splits in reading books or textbooks is confused... If you have this question, please see my explanation of the example below. I believe you will have a certain understanding of this.
Let me first introduce the usage of the Split function:
Return value array = Split("string","split")
Suppose the variable strURL holds the URL value, such as strURL="ftp://username:password@server", which is the URL form when we log in to FTP in IE. What should we do if we want to take out the username and password in it? Of course, there are many solutions, here we will only introduce the solutions using Split. First, we find the splitter. We found that in this string, there is a colon between username and password that separates them, so we use this colon as the "segment" of the Split function to divide the entire string, and finally achieve the purpose of taking username and password. The code is as follows:
strURL="ftp://username:password@server"
aryReturn=Split(strURL,":")
In this way, we split the string with a colon, and the result after segmentation is saved in aryReturn (aryReturn is an array).
Let's take a look at the final result. Because the Split function returns an array in the end, we mainly display the elements in the array, which involves some functions related to arrays: IsArray() function to determine whether an array is an array, LBound() takes the subscript of the array, and UBound() takes the superscript of the array.
Response.Write("Is the return value an array:"&IsArray(aryReturn)&"<br>")
Fori=LBound(aryReturn)ToUBound(aryReturn)
Response.Write("Return element in value array["&i&"]: "&Right(aryReturn(i),Len(aryReturn(i))-2)&"<br>")
Next
Through the above code, we see that the string is divided into three parts, namely: "ftp", "//username", and "password@server". We need to process further when we want to get username and password, so I won’t say much, just give the code.
Get username code:
strUsername=Right(aryReturn(1),Len(aryReturn(1))-2)
Take password code:
'We used the Split function again to take password, but this time the splitter is "@"
aryTemp=Split(aryReturn(2),"@")
strPassword=aryTemp(0)
'We can take out the server
strServer=aryTemp(1)
A splitter can be a character or a string. like:
aryReturn=Split("ftp://username:password@server,"//")
Notice:
1. Generally speaking, variables can be not declared in ASP. When using the Split function, if you want to declare the variable with the return value, you can only use Dim, not Redim. Although it is returned as an array, it should be possible to use Redim, but it is not possible during actual use. I don't know what's going on?