Have you ever encountered the situation of wanting to get certain values from a string but not knowing where to start? Have you ever felt confused about how to write split in books or textbooks? If you have this question, please read 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","separator")
Assume that the variable strURL stores the URL value, such as strURL = " ftp://username:password@server ". This is the URL form when we log in to FTP in IE. If we want to take out the username and password , what to do? Of course, there are many solutions. Here we only introduce the solution using Split. First, we find the delimiter. We found that in this string, there is a colon between username and password, so we use this colon as the "separator" of the Split function to split the entire string, and finally achieve the purpose of fetching username and password. . The code is as follows:
strURL = " ftp://username:password@server "
aryReturn = Split(strURL,":")
In this way, we split the string with colons, and the split results are stored in aryReturn (aryReturn is an array).
Let's take a look at the final result. Because the Split function ultimately returns an array, we mainly display the elements in the array, which involves some array-related functions: IsArray() determines whether it is an array. Function, 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>")
For i = LBound(aryReturn) To UBound(aryReturn)
Response.Write("Elements in the return value array [" & i & "]: " & Right(aryReturn(i),Len(aryReturn(i))-2) & "<br>")
NextThrough
the above code, we see that the string is divided into three parts, namely: "ftp", "//username", " password@server ". We need to further process the username and password, so I won’t go into details and give the code directly.
Get the code of username:
strUsername = Right(aryReturn(1),Len(aryReturn(1))-2)
The code to get the password:
'We use the Split function again to get the password, but this time the separator is "@"
aryTemp = Split(aryReturn(2),"@")
strPassword = aryTemp(0)
'We can take out the server by the way
strServer = aryTemp(1)
The delimiter can be a character or a string. like:
aryReturn = Split(" ftp://username:password@server,"// ")
Note:
1. Generally speaking, variables do not need to be declared in ASP. When using the Split function, if you want to declare a variable that returns a value, you can only use Dim, not Redim. Although it is said that the return is an array, it should be possible to use Redim, but it is not possible in actual use. I don’t know what’s going on?
2. If the Split function is used to split a string with a separator that does not exist, the entire string will be returned, and the result is an array with only one element.
Later, if you want to extract certain characters or parts from a string, as long as you grasp the rules and use split, you can achieve various effects. I write this article, hoping it will be helpful to everyone's study, and I also hope that experts from all walks of life can give some advice!