473,732 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_st ring) + 1, i don't know but
nevermind that) but what if a searched_string == '' ? Then
string.find(sub string,searched _string) == 0 ?

example:
import string
searched_string ="abcdefg"
substring="123"
print string.find(sub string,searched _string) -1 searched_string =""
print string.find(sub string,searched _string) 0

why would this be? And when is someone going to fix it :P
- Haz

Jul 18 '05 #1
5 1840
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_st ring) + 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(sub string,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(sub string,searched _string) 0
why would this be? And when is someone going to fix it :P

substring[0:0+len(searche d_string)] == searched_string

True

</F>

Jul 18 '05 #3
MyHaz wrote:
import string
searched_st ring="abcdefg"
substring=" 123"
print string.find(sub string,searched _string) -1
searched_st ring=""
print string.find(sub string,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(sea rched_string, substring) # note the order

searched_string =""
print string.find(sea rched_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
51723
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 have some legcay C code that needs to process a string taken from a textbox, then I need to re-display the string as the textbox->Text. I easily found how to convert from system::string to char but I can't figure out how to go the other way!!
108
6429
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 describe if applied to a sequence of length items. It returns a tuple of three integers; respectively these are the /start/ and /stop/ indices and the /step/ or stride length of the slice. Missing or out-of-bounds indices are handled in a manner...
12
13583
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!"; int size1 = sizeof(std::string); int size2 = sizeof(someString); and printed out the values of size1 and size2. size1 and size2 always
2
4548
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, what is the syntax?
5
3138
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 navigated from the folder indicated. In .Net, however, when running through the IDE, this seems to return either the ...bin/debug or ...bin/release folder - in which case the 'sub-folders' are actually to be found in the .../../ folder.
10
1246
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 would do this: Dim myString as string="string" msgbox (mystring.substring(0,9))
6
1506
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 manged memory. Because it has value in inner function, but does not have the assigned value in outher function. So I need to know how to declare this variable and make assignments correctly so that it will have value when it is back in outher...
7
373
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 at s1, so s1 should end at s1, right?
5
23469
by: SMichal | last post by:
Hi, how can I parse string "? 20.000" to double ?
0
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9307
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.