444,035 Members | 1,324 Online
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

# oddness in string.find(sub,somestring)

 P: n/a OK i find this a quark in string.find that i think needs some consideration. Normally if a substring is not in the searched_string then string.find returns -1 (why not len(searched_string) + 1, i don't know but nevermind that) but what if a searched_string == '' ? Then string.find(substring,searched_string) == 0 ? example: import string searched_string="abcdefg" substring="123" print string.find(substring,searched_string) -1 searched_string="" print string.find(substring,searched_string) 0 why would this be? And when is someone going to fix it :P - Haz Jul 18 '05 #1
5 Replies

 P: n/a MyHaz wrote: OK i find this a quark in string.find that i think needs some consideration. You shouldn't use the module string anymore, instead use string-methods. Normally if a substring is not in the searched_string then string.find returns -1 (why not len(searched_string) + 1, i don't know but Because then you need to know how large that string is. That makes the case-distinction much easier - as -1 can never be the position of a substring. Besides, a value of len(haystack) + 1 says "The needle is beyond that string" - which is bogus. nevermind that) but what if a searched_string == '' ? Then string.find(substring,searched_string) == 0 ? "".find("fooob") -1 as expected. Which python version do you use? -- Regards, Diez B. Roggisch Jul 18 '05 #2

 P: n/a "MyHaz" wrote: searched_string="" print string.find(substring,searched_string) 0 why would this be? And when is someone going to fix it :P substring[0:0+len(searched_string)] == searched_string True Jul 18 '05 #3

 P: n/a MyHaz wrote: import stringsearched_string="abcdefg"substring="123"print string.find(substring,searched_string) -1searched_string=""print string.find(substring,searched_string) 0 why would this be? And when is someone going to fix it :P I don't know. When are you going to fix it? -- hilsen/regards Max M, Denmark http://www.mxm.dk/ IT's Mad Science Jul 18 '05 #4

 P: n/a MyHaz wrote: why would this be? And when is someone going to fix it :P Whoops ... This missed the previous message together with the wink ... ;-) "When *are* you going to fix it ..." substring="123" import string searched_string="abcdefg" print string.find(searched_string, substring) # note the order searched_string="" print string.find(searched_string, substring) You will not make that error if you use the string method instead. searched_string.find(substring) And you don't have to import anything either.. -- hilsen/regards Max M, Denmark http://www.mxm.dk/ IT's Mad Science Jul 18 '05 #5

 P: n/a thanks all that clears things up nicely - Haz Jul 18 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.