Portal    Foro    Buscar    FAQ    Registrarse    Conectarse

Extendiendo La Clase String

Extendiendo La Clase String
Artículo
Responder citando    Descargar mensaje  
Mensaje 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 
tincho - Ver perfil del usuario Enviar mensaje privado  
tincho [ Martes, 05 May 2020, 21:39 ]
 


Extendiendo La Clase String
Comentarios
Responder citando    Descargar mensaje  
Mensaje 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
 




 
cogier - Ver perfil del usuario Enviar mensaje privado  
cogier [ Miercoles, 06 May 2020, 15:11 ]
Responder citando    Descargar mensaje  
Mensaje 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.



 
Guizans - Ver perfil del usuario Enviar mensaje privado  
Guizans [ Miercoles, 06 May 2020, 21:23 ]
Responder citando    Descargar mensaje  
Mensaje 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.



 
tincho - Ver perfil del usuario Enviar mensaje privado  
tincho [ Miercoles, 06 May 2020, 21:26 ]
Mostrar mensajes anteriores:    
 
Publicar nuevo tema  Responder al tema  Página 1 de 1
 
 

Usuarios navegando en este tema: 0 registrados, 0 ocultos y 1 invitado
Usuarios registrados conectados: Ninguno


 



 

cron