4

Find all occurrences and locations of a substring

 2 years ago
source link: https://www.codesd.com/item/find-all-occurrences-and-locations-of-a-substring.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Find all occurrences and locations of a substring

advertisements

I'm writing a program to parse some data saved as text files. What I am trying to do is find the location of every needle in a haystack. I already can read the file in and determine the number of occurrences, but I am looking to find the index also.


string str,sub; // str is string to search, sub is the substring to search for

vector<size_t> positions; // holds all the positions that sub occurs within str

size_t pos = str.find(sub, 0);
while(pos != string::npos)
{
    positions.push_back(pos);
    pos = str.find(sub,pos+1);
}

Edit I misread your post, you said substring, and I assumed you meant you were searching a string. This will still work if you read the file into a string.

Related Articles

find all occurrences and any position of a substring in a string

i need to find all occurrences and found all position of a substring in a string, for example my string is abaaab, my substring is aa, position is 3 and 4, because in aaa my substr is repeated twice, i want the position at the end is print from right

VIM finds all occurrences and replace with the entry

Is it possible to search a file for the occurences of a specific word and ask the user for input to replace this word with. Do the same thing over again, either typing in a new replacement or using something from your history, untill all the words ar

Find all occurrences and replace the string with a pattern match

I want to do a string replace against the following lines. I DONT want to replace any occurrence that isn't displayed within a URL format so secure.XYZ.ab.mydomain.com on a line by itself or in variable format ($myvar = secure.XYZ.ab.mydomain.com) SH

What is the fastest way to find all occurrences of a substring?

This is purely out of curiosity. I was browsing through an article comparing various string search algorithms and noticed they were all designed to find the first matching substring. This got me thinking... What if I wanted to find all occurrences of

Python - Find all occurrences of a substring (including overlap)?

Okay, so I found this: Find all occurrences of a substring in Python Which says, to get the indices overlapping occurances of substrings in a list, you can use: [m.start() for m in re.finditer('(?=SUBSTRING)', 'STRING')] Which works, but my problem i

PHP Find all occurrences of a substring in a string

I need to parse an HTML document and to find all occurrences of string asdf in it. I currently have the HTML loaded into a string variable. I would just like the character position so I can loop through the list to return some data after the string.

Find all occurrences of a letter in a string with offsets

I have a string as follows: --d--d-d---d-- I want to find all occurrences of 'd' in this string with their offsets. However, doing the following only gives me back the first result: irb(main):001:0> m = /d/.match "d--d-d---d" => #<Match

What is the worst-case complexity for KMP when the goal is to find all occurrences of a certain string?

I would also like to know which algorithm has the worst case complexity of all for finding all occurrences of a string in another. Seems like Boyer–Moore's algorithm has a linear time complexity.The KMP algorithm has linear complexity for finding all

PHP regex templating - find all occurrences of {{var}}

I need some help with creating a regex for my php script. Basically, I have an associative array containing my data, and I want to use preg_replace to replace some place-holders with real data. The input would be something like this: <td>{{address}}

Find all occurrences of text in different enclosures

We have an acronym which has specific casing. Business now wants us to find all occurrences where the casing is wrong and fix it. Example of correct casing: HtMl The search operation would then need to return all occurrences of HTML, html, Html, HtML

Find all duplicates and missing values ​​in a sorted array

Suppose you have a sorted range (x to y) of values in an array. x = 3; y = 11; array == 3, 4, 5, 6, 7, 8, 9, 10, 11 But it is possible that some values are duplicated and some are missing, so you might have: array == 4, 5, 5, 5, 7, 8, 9, 10, 10 What'

Find all tables and fields on & ldquo; many & rdquo; side of relations with a certain table

in Sql Server 2005, I have a master table, and several other tables which are related to this master through several one to many relations. How can I find all tables and fields which are in relation with the primary key in the master table, on "many&

Find all substrings and repeating non-overlapping cycles

I have a complex problem of string manipulation at hand. I have a string in which I will have cycles, as well as recurrences which I need to identify and list down. 'abcabcabcabcabcdkkabclilabcoabcdieabcdowabcdppabzabx' Following are the possible pat

Find the frequency and location of a recurring word in an excel cell

How can I find() the location of the Nth occurrence of a word in a sentence in a cell? Example: Humpty Dumpty sat on a wall. Humpty had a great fall. In above i want to find the total occurrences and the location of the word 'Humpty'. Expected Result

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK