473,466 Members | 1,430 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Search substring in a string and get index of all occurances

Hi there,

I would like to search for a substring in a string and get the index of
all occurances.

mystring = 'John has a really nice powerbook.'
substr = ' ' # space

I would like to get this list:
[4, 8, 10, 17, 22]

How can I do that without using "for i in mystring" which might be
expensive for large strings?

Thanks in advance,
Nico
Jun 21 '06 #1
1 3051
mystring = 'John has a really nice powerbook.'
substr = ' ' # space

pos = 0
indices = []
while True:
i = mystring.find(substr,pos)
if i==-1:
break
indices.append(i)
pos = i+1
print indices
[4, 8, 10, 17, 22]


Pierre

Jun 21 '06 #2

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

Similar topics

5
by: lawrence | last post by:
I've waited 6 weeks for an answer to my other question and still no luck, so let me rephrase the question. I know I can do this: <form method="post" action="$self"> <input type="text"...
2
by: Christian Hammers | last post by:
Hello How can I do the following command with PHP? $ snmpwalk -Ir -On myhost \ 'ASCEND-MIBINET-MIB::internetProfile-Active."cir-1-26"' ..1.3.6.1.4.1.529.23.1.1.1.2.8.99.105.114.45.49.45.50.54...
38
by: Alan | last post by:
I want to change a 3 digits integer to characters, how can i do that? the 3 digits integer maybe 123, 23 or 3 I want to change the integer to "123", " 23" or " 3" thx
6
by: Alan | last post by:
I want to search a string in a file, but I don't know the position, how can I search the string and stop at that line containing that string ? for example, I want to seach the string ".ABC" in a...
2
by: Lian | last post by:
Hi I am making a console application, which I need to search a string with google . How can I do it? Liran
4
by: ad | last post by:
When we user SqlDataReader, we can use integer or string as index. For example, if the query string is : Select Math, Nature, Social form Scores" and the myRD is the SqlDataReader, We can refer...
3
by: Woody Splawn | last post by:
For reasons I have not yet identified my Help, Search and Help, Index has quit working. That is, in the VB IDE, from the Help menu, I can select Dynamic help or Contents and things work as...
3
by: diablo | last post by:
Hi if i have a string with comma seperated words. is there a way to search another string for words that are in it? thanks Kal
4
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, can someone please show me how to search my String array to see if an element "contains" a certain string constant and then return the index value of the found element? thanks, rodchar
5
by: jamesnkk | last post by:
How do I search a string - I have a ms access table consist of 2 columns - "Part Nos"..........."Components" K123456............. U123,Q545,U5,Q3321,U22 ...
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
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,...
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...
1
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...
0
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,...
0
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...
0
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...
0
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...

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.