473,698 Members | 2,337 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 1836
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
51713
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
6390
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
13575
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
4545
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
3135
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
1242
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
1503
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
23467
by: SMichal | last post by:
Hi, how can I parse string "? 20.000" to double ?
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8608
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
9161
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
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...
1
8897
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6522
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
5860
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
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
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

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.