473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

oddness in string.find(sub,somestring)

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 1819
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
"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

</F>

Jul 18 '05 #3
MyHaz wrote:
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


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
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
thanks all that clears things up nicely
- Haz

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
12
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!";...
2
by: Ray Stevens | last post by:
I would like to quickly strip out all non-numeric characters from a string (including whitespace) into a new string (i.e., remove the edit mask). Would RegEx be the best way to do this and, if so,...
5
by: Robert Magnusson | last post by:
Hi All, This is sure to be an easy question. I am wondering what the accepted standard is for referencing a sub-folder below the application.exe folder? In VB6 you simply used App.Path and...
10
by: MS Newsgroups | last post by:
Hi, I have a feeling there is a easy answer to this. I need to return the first 9 characters in a string if it is longer than 9 characters, otherwise the whole string. If was hoping this code...
6
by: brian_harris | last post by:
I have a function that passes a string class pointer to a function, this function is then suppose to fill it and the outer function uses it. But I believe that I am running into problem due to...
7
by: junw2000 | last post by:
In the following code: std::string s1; s1 = "ABCDE"; std::cout<<s1<<std::endl; s1 = '\0'; //LINE1 std::cout<<s1<<std::endl; //LINE2 At LINE1, I add a NULL terminator...
5
by: SMichal | last post by:
Hi, how can I parse string "? 20.000" to double ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.