Extendiendo La Clase String


Subject: Extendiendo La Clase String
Hola a todos.
En base a otro hilo en donde comentamos sobre algunos métodos que le "faltaban" a la clase String pongo aquí como la extendí.
' gambas class file
'' This class extends the standard String class in gambas
Export
Create Static
Inherits String
'' This function counts how much times some string apear in other
Public Function StrCount(sWhere As String, sWhich As String) As Integer
Dim i As Integer
Dim Cursor As Integer
Dim Counter As Integer
i = 1
Repeat
Cursor = InStr(sWhere, sWhich, i)
If Cursor > 0 Then
Inc Counter
i = i + Cursor + String.Len(sWhich)
Endif
Until InStr(sWhere, sWhich, i) = 0 Or i > String.Len(sWhich)
Return Counter
End

Espero les sea de utilidad.
Saludos.

Last edited by tincho on Tuesday, 05 May 2020, 21:40; edited 1 time in total
Profile PM  
Subject: Re: Extendiendo La Clase String
Parece que hay algunos errores en su código. Intenta con el siguiente código: -

Public Sub Form_Open()

Dim sString As String = "Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World"

Print StrCount(sString, "World")

End

Public Function StrCount(sWhere As String, sWhich As String) As Integer

Dim i As Integer = 1
Dim Cursor As Integer
Dim Counter As Integer

'i = 1
Repeat
Cursor = InStr(sWhere, sWhich, i)
If Cursor > 0 Then
Inc Counter
'i = i + Cursor + String.Len(sWhich)
i = Cursor + String.Len(sWhich)
Endif
Until InStr(sWhere, sWhich, i) = 0 Or i > String.Len(sWhere)
'Until InStr(sWhere, sWhich, i) = 0 Or i > String.Len(sWhich)
Return Counter

End

Profile PM  
Subject: Re: Extendiendo La Clase String
Hola.

Mira que casualidad que necesité una función igual a esa hace poco, y también cree una clase extendida de String.
Public Function Count(palabra As String, caracter As String, Optional inicio As Integer = 1, Optional final As Integer = 0) As Integer ''Devuelve el número de coincidencias de una subcadena en el rango [inicio, fin].

Dim longitud As Integer
Dim cortado As New String[]
Dim posicion As Integer[]
Dim palabraS As String

longitud = String.Len(palabra)
If final = 0 Then final = longitud

palabraS = String.Mid$(palabra, inicio, final)

posicion = FindNumber(palabraS, caracter)

For Each a As Integer In posicion
cortado.Add(String.Mid$(palabraS, a, String.Len(caracter)))
Next

Return cortado.Length

End

Public Function FindNumber(palabra As String, sufijo As String) As Integer[] ''Encuentra en que posiciones de un string esta el sufijo devolviendo un array que contendrá las posiciones

Dim a As Integer
Dim pos As Integer = 0
Dim posicion As New Integer[]

While True
a = InStr(palabra, sufijo, pos)
If a = 0 Then
Break
Else
posicion.Add(a)
pos = a + 1
Endif
Wend

Return posicion

End


En este caso si que devuelve bien el número de coincidencias del ejemplo de cogier.

Un saludo.

Profile PM  
Subject: Re: Extendiendo La Clase String
cogier escribió:  
Parece que hay algunos errores en su código. Intenta con el siguiente código...[/code]

Gracias Cogier.
Saludos.

Profile PM  

Page 1 of 1


  
You cannot post new topics
You cannot reply to topics
You cannot edit your posts
You cannot delete your posts
You cannot vote in polls
You cannot attach files
You can download files
You cannot post calendar events

   

This is a "Lo-Fi" version of our main content. To view the full version with more information, formatting and images, please click here.

Powered by Icy Phoenix based on phpBB
Design by DiDiDaDo

Generation Time: 0.1241s (PHP: -18% SQL: 118%)
SQL queries: 24 - Debug Off - GZIP Enabled