This section describes how to use the split function:

Source: Internet
Author: User
Have you ever thought of getting some values in a string without starting? Are you confused about how to write split in a book or textbook ...... If you have any questions, please refer to 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:
Returned value array = Split ("string", "delimiter ")

Assume that the variable strurl stores the URL value, for example, strurl = "ftp: // username: password @ server". This is the URL format when we log on to FTP in IE, what should we do if we want to obtain the username and password? Of course there are many solutions. Here we will only introduce how to solve this problem using split. First, we can find the delimiter. In this string, there is a colon between username and password to separate them, so we use this colon as the "delimiter" of the split function to separate the entire string, finally, the username and password are obtained.CodeAs follows:
Strurl = "ftp: // username: password @ server"
Aryreturn = Split (strurl ,":")

In this way, the string is separated by a colon, and the split result is saved in aryreturn (aryreturn is an array ).

Next let's take a look at the final result, because the split function returns an array, So we mainly display the elements in the array, which involves some functions related to the array: isarray () is a function used to determine whether an array is used. lbound () is used to obtain the subscript of an array, and ubound () is used to obtain the supermark of an array.

Response. Write ("Whether the returned value is an array:" & isarray (aryreturn) & "<br> ")
For I = lbound (aryreturn) to ubound (aryreturn)
Response. write ("elements in the returned value array [" & I & "]:" & right (aryreturn (I), Len (aryreturn (I)-2) & "<br> ")
Next

The code above shows that the string is divided into three parts: "ftp", "// username", and "password @ server ". We need to take username and password for further processing. I will not talk about it, but we will give the code directly.
Code for getting Username:
Strusername = right (aryreturn (1), Len (aryreturn (1)-2)
Password code:

'The password is used again by the split function, but the delimiter for this time is "@"
Arytemp = Split (aryreturn (2 ),"@")
Strpassword = arytemp (0)
'Let's retrieve the server by the way.
Strserver = arytemp (1)

A delimiter can be a character or a string. For example:
Aryreturn = Split ("ftp: // username: password @ server ,"//")

Note:
1. In general, ASP does not declare variables. When using the split function, if you want to declare the returned variables, you can only use dim instead of redim. Although the returned result is an array, redim can also be used, but it cannot be used in actual use. I don't know what's going on?
2. If the split function is used to separate non-existent delimiters in a string, the entire string is returned and the result is an array of only one element.

later, to take some characters or parts of a string, you only need to grasp the Regular Expression and use split to make a variety of effects. I hope this article will be helpful to everyone's learning. I also hope you can give some advice!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.