faqts : Computers : Programming : Languages : Tse : String

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

Entry

TSE: String: Count: How to count the total amount of occurrences of a string in another string?

Nov 7th, 2006 16:19
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 08 November 2020 - 01:16 am -------------------
TSE: String: Count: How to count the total amount of occurrences of a 
string in another string?
===
Method: Use Pos() repeated times
Using Pos() you find the first occurrence position.
When found the string, you go past the string at that
position and repeat the search again.
===
--- cut here: begin --------------------------------------------------
// library: string: get: count (filenamemacro=getstgco.s) [kn, ri, we, 
08-11-2020 01:15:00]
INTEGER PROC FNStringGetCountI( STRING containS, STRING searchS )
 INTEGER I = 1 - 1
 INTEGER countI = 1 - 1
 INTEGER foundB = FALSE
 INTEGER beginI = 1
 INTEGER endI = Length( containS )
 INTEGER lengthSearchI = Length( searchS )
 REPEAT
  I = Pos( searchS, containS[beginI..endI] )
  foundB = ( I > 0 )
  IF foundB
   countI = countI + 1
   beginI = beginI + I + lengthSearchI - 1
  ENDIF
 UNTIL NOT foundB
 RETURN( countI )
END
PROC Main()
 Set( break, ON )
 Warn( FNStringGetCountI( "ABCDFG", "C" ) ) // gives 1, because "C" 
can found 1 time in "ABCDFG"
 Warn( FNStringGetCountI( "ABCCDFG", "C" ) ) // gives 2, because "C" 
can found 2 times in "ABCCDFG"
 Warn( FNStringGetCountI( "ABCCCCDFG", "C" ) ) // gives 4, because "C" 
can found 4 times in "ABCCCCDFG"
 Warn( FNStringGetCountI( "TestTestTest", "Test" ) ) // gives 3, 
because "Test" can found 4 times in "TestTestTest"
END
<F12> Main()
--- cut here: end ----------------------------------------------------
===
Method: Use StrCount()
TSE v4.x
In TSE v4.x there is an inbuilt function StrCount()
See TSE v4.x help for more information.
===
Internet: see also:
---
----------------------------------------------------------------------