Write concise code with the InStr function

You can often use the Instr function in an unorthodox way to write more concise code. A typical example is when you need to test a single character:

' test whether CHAR contains a vowel' the standard wayIf UCase$(char) = "A" Or UCase$(char) = "E" Or UCase$(char) = "I" Or UCase$ _    (char) = "O"  Or UCase$(char) = "U" Then        ' it is a vowelEnd If' the more concise solutionIf InStr("AaEeIiOoUu", char) Then    ' it is a vowelEnd If

You can also use InStr to check the contents of a variable against a list of multi-character words, using a delimiter character that can’t appear in the word:

' test whether WORD contains the name of a season' the standard wayIf LCase$(word) = "winter" Or LCase$(word) = "spring" Or LCase$(word) = _    "summer" Or LCase$(word) = "fall" Then        ' it is a season's nameEnd If' the more concise wayIf Instr(";winter;spring;summer;fall;", ";" & word & ";") Then    ' it is a season's nameEnd If

In some sames you can even use InStr to replace a Select Case block, but you must pay a lot of attention to the number of characters in the arguments:

' convert a string in the range from "zero" to "nine" to the corresponding ' numeric value:' the standard waySelect 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 = 9End Select' the more concise way' note that the longest word in the list is 5 characters long, so we must' pad shorter strings with the necessary number of semicolonsresult = InStr(";zero;;one;;;two;;;three;four;;five;;six;;;seven;eight;nine;", _    ";" & LCase$(word) & ";")  6

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a