Forum Moderators: open
Here's what I'm trying to do.
I'm starting with a string like that could be one of the following:
1. A wolf ate my widget
2. My fox and my widget are friends
3. A parrot talks to my widget
My code needs to do one of the following:
1. If the string contains the word 'wolf' then it should output the word 'wolf'.
2. If the string contains the word 'fox' then it should output the word 'fox'.
3. If the string contains the word 'parrot' then it should output the word 'parrot'.
4. If the string doesn't contain any of the above then the string should then be output in full.
Chris.
Don't we all love widgets ;). The widget - the greatest and most useful word on the planet.
In c#, it would be
private void btnTest_Click(object sender, System.EventArgs e)
{
MessageBox.Show(ProcessString("A wolf ate my widget"));
MessageBox.Show(ProcessString("My fox and my widget are friends"));
MessageBox.Show(ProcessString("A parrot talks to my widget"));
MessageBox.Show(ProcessString("A groundhog talks to my widget"));
}private String ProcessString(String strIn)
{
String[] astrWords = new String[] {"wolf", "fox", "parrot"};
String strOut = strIn;
foreach (String strWord in astrWords)
{
if (strIn.IndexOf(strWord, 0) >= 0)
{
strOut = strWord;
break;
}
}
return strOut;
}
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
MessageBox.Show(ProcessString("A wolf ate my widget"))
MessageBox.Show(ProcessString("My fox and my widget are friends"))
MessageBox.Show(ProcessString("A parrot talks to my widget"))
MessageBox.Show(ProcessString("A groundhog talks to my widget"))
End SubPrivate Function ProcessString(ByVal strIn As String) As String
Dim astrWords As String() = New String() {"wolf", "fox", "parrot"}
Dim strOut As String = strIn
Dim strWord As String
For Each strWord In astrWords
If strIn.IndexOf(strWord, 0) >= 0 Then
strOut = strWord
Exit For
End If
Next
Return strOut
End Function
Here's the deal.
When a user visits my site I'm requesting their UserAgent. If it contains either 'MSIE', 'Netscape' or 'Opera' then I want the output to be the browser and version number. Otherwise, I want the entire UserAgent string to be the output.
The reason I want this is I am developing a whole new intelligent logging system to spead across all my 2,000 and upwards sites so I can publish acturate and detailed stats on which browsers, operating systems and screen resolution people are using. Of course wmw (and anyone else) will have access to this info if they want.
Thanks for your help.
Chris.
Dim br As HttpBrowserCapabilities = Request.Browser
Dim sBrowserName As String = UCase(br.Browser)
Dim sVersion As String = br.Version
Select Case sBrowserName
Case "IE"
GetBrowser = "This is IE browser, version: " + sVersion
Case "OPERA"
GetBrowser = "This is Opera browser, version: " + sVersion
case .....
End Select
End Function
Before we go further, I think we need all the parameters. Are you using regular ASP or ASP.NET? Then, what are you going to do with the results? Write them to memory, a database, an XML file? Do you want just the reported browser, or if it is Opera lying to say that it is IE, do you want to pick that up?
I think I'm making this much harder than it needs to be. When a visitor hits my page I want to log their visit in a Microsoft Access database using ASP.net/VB.net. I previously asked about dealing with Opera here [webmasterworld.com]. It shows that opera still identifys itself even when sending another browsers UserAgent.
Basically, I want the field in the database be as clean as possible. If it is 'Internet Explorer', 'Netscape' or 'Opera' I want the field to be in the following format.
{browser name} {version}
e.g. Internet Explorer v.5.5
If it is none of the above, I what the entire UserAgent to be copied into the field. I can clean up the code at a later date to deal with identifying the bots
Thanks for your help
Chris
I've just read you post, and although I've thought of using Browser and Version instead of UserAgent, I need my code to be tight. What, for instance, would be registered if GoogleBot visited? I need all circumstanced to be catered for. What would happen if a new browser came on the seen?
Thanks,
Chris.
How do I search a string stored in a variable?
If it where a database field I would do:
Select * from table where field='%Opera%'
I obviously can't do that because it's stored as a variable and not in a database. How can I do it in a variable?
Chris.
My code is working correctly except when Opera is used and identified as Opera
--- code snippet ---
Dim InWord1, InWord2, InWord3, InWord4 As Integer
Dim Word1, Word2, Word3, Word4 As StringWord1 = "IE"
Word2 = "Netscape"
Word3 = "Opera"
Word4 = "[en]"InWord1 = UserAgent.IndexOf(Word1)
InWord2 = UserAgent.IndexOf(Word2)
InWord3 = UserAgent.IndexOf(Word3)
InWord4 = UserAgent.IndexOf(Word4)If InWord1 > 0 Then
TypeOfBrowser.Text = "IE detected"
End If
If InWord2 > 0 Then
TypeOfBrowser.Text = TypeOfBrowser.Text & "Netscape detected"
End If
If InWord3 > 0 Then
TypeOfBrowser.Text = TypeOfBrowser.Text & "Opera detected"
End If
If InWord4 > 0 Then
TypeOfBrowser.Text = TypeOfBrowser.Text & "[en] detected"
End If
When Opera is used and identified as Opera InWord3 has the value 0 which means "Opera detected" is not displayed.
However, the UserAgent is
Opera/6.03 (Windows XP; U) [en]
Can anyone see why?
Chris