Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using InStr and Select Case help 2

Status
Not open for further replies.

EchoAlertcom

IS-IT--Management
Oct 8, 2002
239
US
Hello,

I need to use InStr and Select Case together and I'm not sure what to do. I would like to process a string and do something depending on which word is found. Could someone suggest where I'm going wrong. Or suggest an all-around better way to do it please?

Code:
strLine = "Georgia is on the East Coast"


Select Case strLine

	Case If InStr(strLine, "Florida") > 0 Then
		
		Response.Write "Florida"
		
	Case If InStr(strLine, "Georgia") > 0 Then
	
		Response.Write "Georgia"
		
	Case If InStr(strLine, "Alabama") > 0 Then
	
		Response.Write "Alabama"

	Case Else
		
		Response.Write "Other States"
		
End Case

Thank you,
Steve
 
This is how it can be done.

[tt]strLine = "Georgia is on the East Coast"
Select Case true
Case InStr(1,strLine, "Florida",1) <> 0
Response.Write "Florida"
Case InStr(1,strLine, "Georgia",1) <> 0
Response.Write "Georgia"
Case InStr(1,strLine, "Alabama",1) <> 0
Response.Write "Alabama"
Case Else
Response.Write "Other States"
End Select
[/tt]
Notes: [1] Testing "<>0" is more efficient. [2] Add parameters (1,.,.,1) for case insensitivity.
 
Using an array may be even more efficient:[tt]

strLine = "Georgia is on the East Coast"
arrWords = Split(strLine, " ")
For x = 0 to uBound(arrWords)
strState = arrWords(x)
Select Case strState
Case "Florida"
Exit For
Case "Georgia"
Exit For
Case "Alabama"
Exit For
Case Else
strState = "Other States"
End Select
Next
Response.Write strState
[/tt]
 
>Using an array may be even more efficient:
I don't think so here. If one knows the place being the first word, why testing x=0 to ubound()? Just test data at x=0? No intention to ridiculize the method which is valid---I don't disagree. Efficient? I don't think so... But this is such a small matter that I wouldn't insist or convert even proven being wrong.
 
I was thinking along the lines that calling InStr() for each state would be slower because it would parse the string for each state. So if there only 3 states this is not so bad but if there are 50 states then it could be improved.
 
Thank you both. It worked great.

I especially like the tip about case insensitivity.

Warmest Regards,
Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top