https://foro.gambas-es.org/viewtopic.php?f=5&t=8014&p=48305#p48305
-----------------------------------
Shell
Lunes, 04 May 2020, 22:12

Re: Cuantas Veces Se Repite Una Cadena En Otra.
-----------------------------------
He intentado hacerlo con recursividad. 

Aunque no me quedo contento.

[code linenumbers=false syntax=gambas]
'Global variable dependency with the first version of the function
Private counter As Integer

Public Sub Main()
  
  Dim $sentence As String
  Dim $find As String  
  
  $sentence = "Esto es un texto, esto es un texto, esto es un texto."
  $find = "texto"
  
  Print StrCountV2($sentence, $find) 
  
End

'' Function that count the times a substring appear in other string
Public Function StrCountV1(sWhere As String, sWhich As String) As Integer
  
  Dim $pos As Integer
  
  $pos = String.InStr(sWhere, sWhich)
  If $pos > 0 Then
    counter += 1
    sWhere = String.Mid(sWhere, $pos + 1)
    Return StrCountV1(sWhere, sWhich)
  Else
    Return counter
  Endif
  
End

'' Function that count the times a substring appear in other string
Public Function StrCountV2(sWhere As String, sWhich As String, Optional $counter As Integer = 0) As Integer
  
  Dim $pos As Integer
  
  $pos = String.InStr(sWhere, sWhich)
  If $pos > 0 Then
    $counter += 1
    sWhere = String.Mid(sWhere, $pos + 1)
    Return StrCountV2(sWhere, sWhich, $counter)
  Else
    Return $counter
  Endif
  
End
[/code]

Saludos


