Using InStr function to achieve code weight loss

Source: Internet
Author: User
A function can use the InStr function to implement the simplicity of the code in a "Heterodoxy" way. The following is a typical example of whether a vowel letter is included in the string:

1, the common method:


If ucase$ (char) = "A" or ucase$ (char) = "E" or ucase$ (char) = "I" or ucase$ (char) = "O" or ucase$ (char) = "U" Then

' It is a vowel

End If


2. A more concise approach:

If InStr ("Aaeeiioouu", Char) Then

' It is a vowel

End If

Similarly, use InStr to check the contents of a variable by using a character that is not in the word as a delimiter. The following example checks to see if Word contains the name of a season: 1. Common method:

If lcase$ (Word) = "Winter" or lcase$ (word) = "Spring" or lcase$ (Word) = _ "Summer" or lcase$ (word) = "Fall" Then

' It is a season ' s name

End If


2. A more concise approach:

If Instr ("; winter;spring;summer;fall;", ";" & Word & ";") Then

' It is a season ' s name

End If

Sometimes, you can even use InStr to replace a select

Case code snippet, but be sure to note the number of characters in the argument. In the following example, the corresponding English name for the conversion number 0 to 9 is Arabic numerals: 1, the normal method:


Select case lcase$ (word)

Case "Zero"

result = 0

Case "One"

result = 1

Case "Two"

result = 2

Case "three"

result = 3

Case "Four"

result = 4

Case "Five"

result = 5

Case "Six"

result = 6

Case "Seven"

result = 7

Case "Eight"

result = 8

Case "Nine"

result = 9

End Select

2. A more concise approach:

result = InStr ("; zero;;o ne;;; two;;; Three;four;; five;; six;;; Seven;eight;nine; ","; "& lcase$ (Word) &"; " \ 6

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.