Have you ever encountered the need to take some of the values in a string and not know what to do? There is no feeling that reading or teaching materials to split the writing confused ... If you have this question, please see below my explanation to the example, I believe you will have a certain understanding of this.
Let me introduce the use of the split function:
Array of return values = Split ("string", "separator")
Suppose the variable strurl holds the URL value, such as strURL = "Ftp://username:password@server";>ftp://username:password@server<;/a>, This is we in IE login to FTP on the form of the URL, if we want to take out the username and password, what should we do? Of course there are many ways to solve it, here we only introduce the method of using split to solve. First, we find the separator. We found that in this string, there is a colon separating them from the username and password, so we split the colon as a "split" of the split function, and finally get the username and password. The code is as follows:
strURL = "Ftp://username:password@server";>ftp://username:password@server<;/a> "
Aryreturn = Split (strURL, ":")
So we split the string with a colon, and the split result is saved in the Aryreturn (Aryreturn is an array).
Let's take a look at the final result, because the split function eventually returns an array, so we're basically showing the elements in the array, and we're going to involve some functions related to the array: IsArray () to determine whether the array's functions, LBound () take the subscript of the array, UBound () takes the superscript of the array.
Response.Write ("Return value is an array:" & IsArray (Aryreturn) & "
")
For i = LBound (Aryreturn) to UBound (Aryreturn)
Response.Write ("Return the elements in an array of values [" & I &]: "& Right (Aryreturn (i), Len (Aryreturn (i))-2) &"
")
Next
Through the above code, we see that the string is divided into three parts, namely: "FTP", "//username", "Password@server". We want to take username and password need further processing, I will not say more, directly to the code.
To take username code:
strUserName = Right (Aryreturn (1), Len (Aryreturn (1))-2)
To take password code:
' Take password we use the Split function again, but this time the delimiter is ' @ '
Arytemp = Split (Aryreturn (2), "@")
strpassword = arytemp (0)
' We can take the server out of the way
strserver = arytemp (1)
A separator can be a character or a string. Such as:
Aryreturn = Split ("Ftp://username:password@server,"///' >ftp://username:password@server, "//")
Attention:
1. In general, the ASP can not declare variables, use the Split function, if you want to declare the return value of the variable, you can only use dim, but not with ReDim. Although it is said that its return is an array, it should be used ReDim, but in the actual use of the process is not. I don't know what's going on.
2. If the split function is used to split a delimiter that does not exist in a string, the entire string is returned, resulting in an array of only one element.
Something, in order to take a string of certain characters or parts, as long as the grasp of the law, coupled with split can be very good to make a variety of effects. Write this article, I hope to help you learn, but also hope that the road Master can guide twos!