Extendiendo La Clase String


Objetivo: 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.

última edición por tincho el Martes, 05 May 2020, 21:40; editado 1 vez
Perfil MP  
Objetivo: 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

Perfil MP  
Objetivo: 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.

Perfil MP  
Objetivo: 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.

Perfil MP  

Página 1 de 1


  
No puede crear mensajes
No puede responder temas
No puede editar sus mensajes
No puede borrar sus mensajes
No puede votar en encuestas
No puede adjuntar archivos
Puede descargar archivos
No puede publicar eventos en el calendario

   

Está utilizando la versión (Lo-Fi). Para ver la versión completa del foro, haga clic aquí.

Powered by Icy Phoenix based on phpBB
Design by DiDiDaDo

Página generada en:: 0.8532s (PHP: -83% SQL: 183%)
Consultas SQL: 46 - Debug off - GZIP Activado