Sign in to follow this  
Followers 0
ThomasQ

How to count Substrings in String

6 posts in this topic

Hi All!

With the function stringinstr you can find a given substring in a string. You can also set wich occurence of that substring you want found, for instance the 2nd or 3d, etc.. How do I count these substrings in a string, e.q. How do I return the total number of occurences of a substring in a string?

Thanks in advance!

Thomas

Share this post


Link to post
Share on other sites



Local $string = 'avdn123ojvdno 123 vnob123n klbgn123klbcnipg,kl123mbfs'
Local $substring = '123'
Local $nbOccurences

; method 1
StringReplace($string, $substring, $substring)
$nbOccurences = @extended
ConsoleWrite($nbOccurences & @LF)

; method 2
Local $res = StringRegExp($string, $substring, 3)
If @error Then
    $nbOccurences = 0
Else
    $nbOccurences = UBound($res)
Endif
$res = 0        ; you probably don't need that array anymore
ConsoleWrite($nbOccurences & @LF)


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

$sStr = "This is a string" & @CRLF
$sStr &= "This is another part of the string" & @CRLF
$sStr &= "and one more part with another 'this is' in the string."

StringReplace($sStr, "This is", "")
MsgBox(0, "Result", @Extended & " occurences")

Edit: The same method works using StringRegExpReplace() except that it also allows you to check for substringsstrings starting with one character (or word) and ending in another.

StringRegExpReplace($sStr, "(?i)this.*?string", "")
MsgBox(0, "Result", @Extended & " occurences")
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Isn't that a counterfeit copy of method 1 exposed earlier as prior work?

I hope you layer is a friend of yours :(


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Isn't that a counterfeit copy of method 1 exposed earlier as prior work?

I hope you layer is a friend of yours :(

And the edit is a counterfeit copy of method #2.

Just chalk it up to great minds and all that.

And I certainly won't be claiming my versions as intelectual property. That alone would suggest some intelect.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

You're right Georges. My intent in supplying both ways was in the hope the OP would ask about which method to use. It depends of the context: short strings will be certainly faster with straight StringReplace, but that advantage will shrink as the target string gets bigger and, above all, how many replaces are going to occur.

The regexp will keep on looking in the original copy while StringReplace will have to remove $SubString at every match, then start over where it left.

Some weeks ago, I wanted to check how many items were listed on some webpage. Looking at the source, I easily found an html pattern introducing each item. I cranked up a regexp and it gave 393781 occurences. Doing the same with StringReplace would have taken ages.

Also I choose to propose a "do nothing" replace to gain advantage in the (unlikely) case that the developper anticipated the "count substrings" use and placed code to avoid duplication in case of "nop" replace.

Finally, as your edit shows, the regexp can implement possibly complex conditions for matching that StringReplace is obviously unable to check.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0