oddness in string.find(sub,somestring)

M

MyHaz

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:


why would this be? And when is someone going to fix it :p


- Haz
 
D

Diez B. Roggisch

MyHaz said:
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 ?
-1

as expected. Which python version do you use?
 
M

Max M

MyHaz said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

oddness in super() 6
String search 3
How to speed this code 3
Question about sub-packages 0
Strings in Python 6
oddness in shelve module 2
Nested/Sub Extensions in Python 2
Clicking a sub menu item 0

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top