473,659 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

in operator for strings

Wouldn't it be cool if you could do this?

('hello','test' ) in 'blah blah hello blah test':

and it would say if all elements of the list are in the string?

it would be more efficient and more readable than:

'hello' in 'blah blah hello blah test' and 'test' in 'blah blah hello blah
test'
Jul 18 '05 #1
3 1142
On Tue, 25 May 2004 08:56:19 GMT,
"Moosebumps " <mo********@moo sebumps.mb> wrote:
Wouldn't it be cool if you could do this?
('hello','test' ) in 'blah blah hello blah test': and it would say if all elements of the list are in the string? it would be more efficient and more readable than: 'hello' in 'blah blah hello blah test' and 'test' in 'blah blah hello blah
test'


Untested:

def string_contains _any_of( targets, the_string ):
for target in targets:
if target in the_string:
return True
return False

string_contains _any_of( ('hello', 'test'), 'blah blah blah' )

HTH,
Heather

--
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli
Jul 18 '05 #2
"Moosebumps " wrote:
Wouldn't it be cool if you could do this?

('hello','test' ) in 'blah blah hello blah test':

and it would say if all elements of the list are in the string?


well, if you're asking me, I'd say that what's really cool is that you
can do not only

findall(('hello ', 'test'), 'blah blah hello blah test')

but also

find_any(('hell o', 'test'), 'blah blah hello blah test')

find_in_order(( 'hello', 'test'), 'blah blah hello blah test')

find_in_any_cas e(('hello', 'test'), 'blah blah hello blah test')

starts_and_ends _with(('hello', 'test'), 'blah blah hello blah test')

find_only_on_fr idays(('hello', 'test'), 'blah blah hello blah test')

etc.

without having to change the language, or any of its implementations .

</F>


Jul 18 '05 #3
This can be written as:

a=('hello','tes t')
b='blah blah hello blah test'
reduce(lambda x,y: x and b.count(y), a)

or as a fuction:

def contains_all(a, b):
return reduce(lambda x,y: x and b.count(y), a)
Then in main program

if contains_all(a, b):
...

Larry Bates
Syscon, Inc.

"Moosebumps " <mo********@moo sebumps.mb> wrote in message
news:TS******** ***********@new ssvr25.news.pro digy.com...
Wouldn't it be cool if you could do this?

('hello','test' ) in 'blah blah hello blah test':

and it would say if all elements of the list are in the string?

it would be more efficient and more readable than:

'hello' in 'blah blah hello blah test' and 'test' in 'blah blah hello blah
test'

Jul 18 '05 #4

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

Similar topics

6
2357
by: Ruchika | last post by:
Hi, I am new to Python, so this may be a very simple question for most of you. What does the % operator stand for in Python? I came across a script that uses the % operator as follows - def sync(delf,name): os.popen3( 'P4 -s sync -f %s' % name) I would suspect that this just replaces the %s with the value of name.
3
4436
by: Prakash Bande | last post by:
Hi, I have bool operator == (xx* obj, const string st). I have declared it as friend of class xx. I am now able to do this: xx ox; string st; if (&ox == st) { } But, when I have a vector<xx*> and use stl find string xyz;
4
2241
by: Matthias | last post by:
Now I am confused. I am doing a "less" comparison in my program between strings, to see which strings in some range precede others. This is my approach: class SortedByName { public: bool operator() (const boost::filesystem::path& lhs, const boost::filesystem::path& rhs) {
3
17107
by: jl_post | last post by:
Hi, I recently wrote two benchmark programs that compared if two strings were equal: one was a C program that used C char arrays with strcmp(), and the other was a C++ program that used std::strings with operator==(). In both programs, the first string consisted of one million characters (all the letter 'a'). The second string was always one character longer than the first string (with the letter 'a' for all the
17
2504
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: public static bool operator !=(MyType x, MyType y) { return !(x == y); } That way the == operator handles everything, and extra comparing logic isn't
17
1910
by: Jedrzej Miadowicz | last post by:
I recently (re)discovered data binding in Windows Forms thanks to its advances in Visual Studio 2005. As I looked a little deeper, however, I realize that it still suffers from an irksome tendency to stick a whole bunch of literal strings in my code. Quite frankly, I consider them a plague imposed on developers by the RAD designers that come with Visual Studio. The problem with using literal strings to refer to controls, data sources,...
17
2033
by: ma740988 | last post by:
Consider: # include <iostream> # include <algorithm> # include <vector> # include <string> using namespace std; class msg { std::string someStr;
12
2198
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid overloading of == and != but instead translate each call of objA==objB automatically in System.Object.Equals(objA, objB). This would remove inconsistencies like myString1==myString2
8
1683
by: imutate | last post by:
How do you do assignment operator within a template ? #include <vector> template < typename T > class Vec : public std::vector< T { public: Vec() { } Vec( int s ) : std::vector<T>(s) { } T& operator(int i) { return this -at(i); }
1
2697
by: buburuz | last post by:
Hi, I have a question about overloading operator<< . Actually I am trying to understand how it works when chaining multiple calls to this operator. I have a very simple class (MyOut) with an overloaded operator<<, which takes a string as input parameter and returns a reference to an ostream object (std::cout in this case). In the main file I instantiate an object of MyOut and then write a message to console in two different ways: 1....
0
8428
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
8341
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
8751
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
8539
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,...
0
7360
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
6181
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
5650
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
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1982
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.