Forum Moderators: open

Message Too Old, No Replies

Search a string

How do I do it?

         

chris_f

4:40 pm on Jul 6, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, Yet another question. Your all going to get sick of me :).

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.

Xoc

5:25 pm on Jul 6, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You didn't say what language you are working in. But since String is a .NET Framework class, I guess it doesn't matter much.

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;
}


I'll post the same code in Visual Basic .NET in a few minutes.

Xoc

5:34 pm on Jul 6, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In Visual Basic .NET (VB.NET), the same code would be:

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 Sub

Private 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

chris_f

7:49 am on Jul 8, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Xoc,

You are a God.

For reference I always use VB. Should have mentioned that I guess.

Thanks alot. I'll give the code a go tonight.

Chris.

chris_f

5:00 pm on Jul 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry Xoc. I've left one important piece of information out. This is a web application and as such the MessageBox function does not work.

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.

tomasz

6:22 pm on Jul 9, 2002 (gmt 0)

10+ Year Member



Protected Function GetBrowser() As String

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

Xoc

6:50 pm on Jul 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The MessageBox was only the test code, anyway. The function you specified was ProcessString, and works exactly as advertised.

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?

chris_f

1:37 pm on Jul 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry Xoc,

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

chris_f

4:24 pm on Jul 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Tomasz,

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.

chris_f

4:39 pm on Jul 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've figured out the bit of code that is stumping me. Here is the million dollar (or pound) question.

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.

tomasz

4:49 pm on Jul 10, 2002 (gmt 0)

10+ Year Member



Protected Function GetSQL(sBrowser as string) as string
GetSQL=Select * from table where field='%" + sBrowser + "%'
end function

chris_f

4:56 pm on Jul 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks again Tomasz,

But as stated in the post above it is a variable and is not stored in a database. I believe a may have found an article which will help me.

Will let you know

Chris

tomasz

4:57 pm on Jul 10, 2002 (gmt 0)

10+ Year Member



I am just curios what is you database structure, what do you intend to do?
a. insert a record
b. update a record

chris_f

5:43 pm on Jul 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The database will be inserted into a database later. However, at the minute I am just dealling with an ASP variable.

Chris.

tomasz

6:48 pm on Jul 10, 2002 (gmt 0)

10+ Year Member



Chris,

You can use the function Xoc provided

strIn is your variable,

If strIn.IndexOf(strWord, 0) >= 0 Then
'string found
'return whatever
end if

or in ASP instr function
if instr(Variable,String)>0 'found

chris_f

1:43 pm on Jul 11, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks again tomasz,

I'm making now progress.

Chris.

chris_f

1:44 pm on Jul 11, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks again tomasz and xoc,

I'm making now progress.

Chris.

chris_f

4:50 pm on Jul 11, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok. Wierd Problem.

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 String

Word1 = "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

chris_f

5:19 pm on Jul 11, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Never mind. I've found a work around.