Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

String.Contains - Error with custom 'Contains'

gfcwfzkm

  • Please log in to reply
11 replies to this topic

#1 gfcwfzkm

gfcwfzkm

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts
  • LocationBrig-Glis, Switzerland

Posted 24 June 2013 - 01:42 PM

Hi,

 

i want to search in a text some stuff.

i have a message like this:

GET /PIN=TOGGLE HTTP/1.1 Host: 172.17.0.129 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3 Accept-Encoding: gzip, deflate Connection: keep-alive Cache-Control: max-age=0

 

And i want to search for 'Chrome' or 'Safari'. Theres no Safari/chrome in that message, but the Contains-Function give me always a True back. i have my Contains-Function from http://forums.netdui...stringcontains/

    <System.Runtime.CompilerServices.Extension()> _    Public Function Contains(ByVal _src As [String], ByVal _search As Char) As Boolean        For i As Integer = _src.Length - 1 To 0 Step -1            If _src.IndexOf(_search) >= 0 Then                Return True            End If        Next        Return False    End Function

Where is the problem :S

 

mfg

 

gfc

 

I uploaded the Full code, its a webserver for the netduino plus (.MF 4.2)

Attached Files



#2 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 24 June 2013 - 10:46 PM

Hi! I'm not sure but could be the VB implementation of the IndexOf imethod s one-based and that zero means "not found". If so just substitute "greater or equal" for just "greater" or simply replace the zero with a one. Also, your loop repeats the same call to IndexOf for as many times as there are characters in _src - is that intentional and if so, why?

#3 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 25 June 2013 - 03:17 AM

Maybe there is something wrong with the Function for a long string. It returns true on the first test of your message when I run it. Also, the Function is only looking for the first character of the test string, not the whole  word.  Put a Debug.Print(_search) before the test. On all of the cases below it prints "C". Otherwise, the function works OK for me. It finds "C". I would look for another "Contains" function if you want to find a whole word.

Dim Test As String = "Chrome"Dim S As String = "'Some text"Dim S1 As String = "Some text with Chrome"Dim S2 As String = "Some text with chrome"Dim B As BooleanB = S.Contains(Test)Debug.Print("Testing S: " & B.ToString)  '<--FalseB = S1.Contains(Test)Debug.Print("Testing S1: " & B.ToString) '<--TrueB = S2.Contains(Test)Debug.Print("Testing S2: " & B.ToString) '<--False

Also, IndexOf always returns -1 if not found.



#4 gfcwfzkm

gfcwfzkm

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts
  • LocationBrig-Glis, Switzerland

Posted 25 June 2013 - 08:39 AM

I got the answer from http://mikedodaro.ne...icro-framework/ (Blog)

<System.Runtime.CompilerServices.Extension> _Public Shared Function Contains(s As String, value As String) As Boolean    Return s.IndexOf(value) > 0End Function

Problem solved, thx =)



#5 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 25 June 2013 - 08:53 AM

Good, then it was as I suspected and also the solution was the one I suggested earlier:

 

...If so just substitute "greater or equal" for just "greater" or...

 

Also, I see you got rid of the redundant calls to IndexOf (as I also mentioned).



#6 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 25 June 2013 - 09:31 AM

Also, IndexOf always returns -1 if not found.

In C# it does but are you sure it does so in VB too?

It doesn't seem to judging from the solution and VB is traditionally 1-based.



#7 gfcwfzkm

gfcwfzkm

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts
  • LocationBrig-Glis, Switzerland

Posted 25 June 2013 - 02:30 PM

It works good, i tried it in a console application:

The output:

True

s slol

False

Module Module1    Sub Main()        Dim x As String = "s Strb slol"        Console.WriteLine(x.Containsx("Strb").ToString)        x = x.Replace("Strb", "")        Console.WriteLine(x)        Console.WriteLine(x.Containsx("Strb").ToString)        Console.ReadLine()    End Sub    <System.Runtime.CompilerServices.Extension()> _    Public Function Containsx(ByVal s As String, ByVal value As String) As Boolean        Return s.IndexOf(value) > 0    End FunctionEnd Module


#8 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 25 June 2013 - 05:50 PM

In C# it does but are you sure it does so in VB too?

It doesn't seem to judging from the solution and VB is traditionally 1-based.

Well, I always use the construct:

Dim bool As Boolean = If((Not x.IndexOf("Strb") = -1), True, False)orDim bool As Boolean = If((x.IndexOf("Strb") <> -1), True, False)

http://msdn.microsof...y/k8b1470s.aspx

Return Value
Type: System.Int32
The zero-based index position of value if that string is found, or -1 if it is not. If value is String.Empty, the return value is 0.

 

Incidentally, this will not work in the Micro Framework,

 x = x.Replace("Strb", "") <-- ERROR: Replace is not a member of String

You have to use StringBuilder,

 Dim Sb As StringBuilder = New StringBuilder(x) x = Sb.Replace("Strb", "").ToString

 



#9 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 25 June 2013 - 06:37 PM

I see, well then I suppose gfcwfzkm's tests did not run under the micro framework but under the regular. Btw, you do mean "iif" and not just plain old "if", right?

#10 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 25 June 2013 - 10:24 PM

No hanzibal, if you type,  Dim bool As Boolean = if(, IntelliSense will show the syntax. I think IIf is no longer supported; at least, it will not show up with IntelliSense. More ...

http://stackoverflow...28377/iif-vs-if



#11 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 25 June 2013 - 10:38 PM

Ah, I was totally unaware of that, thanks for the info. I really liked the last part about the new operator being "short circuit" so that it only evaluates the true alternative and not both as the old iif did. I guess my VB is gettng a bit rusty from all the C#.

#12 JoopC

JoopC

    Advanced Member

  • Members
  • PipPipPip
  • 148 posts

Posted 26 June 2013 - 01:24 PM

Visual Basic is a very strong language, and I prefer i above the java clone C# because you can write it very organized.

You can push up your production.......

 

 

Also It is not necessary to create so much overhead.

 

Just do things like this:

 

Debug.Print(New StringBuilder("hhxhh").Replace("x", "z").ToString)
 





Also tagged with one or more of these keywords: gfcwfzkm

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.