dim Vi as integer
dim NNN, LLL, LL as string
LL = "El valor que le quieras dar"
For Vi = 1 To Len(LL)
LLL = Mid$(LL, Vi, 1)
If LLL = "1" Then NNN = NNN & LLL
If LLL = "2" Then NNN = NNN & LLL
If LLL = "3" Then NNN = NNN & LLL
If LLL = "4" Then NNN = NNN & LLL
If LLL = "5" Then NNN = NNN & LLL
If LLL = "6" Then NNN = NNN & LLL
If LLL = "7" Then NNN = NNN & LLL
If LLL = "8" Then NNN = NNN & LLL
If LLL = "9" Then NNN = NNN & LLL
If LLL = "0" Then NNN = NNN & LLL
Next
' NNN sera el resultado en numeros
Option Explicit
Private Sub Form_Load()
Debug.Print Only_Numbers("hola1 ¿que34 tal538?")
End Sub
Public Function Only_Numbers(ByRef sText As String) As String
Dim sActualChar As String * 1
Dim lTotalChar As Long
Dim x As Long
lTotalChar = LenB(sText) \ 2
If CBool(lTotalChar) Then
For x = 1 To lTotalChar
sActualChar = Mid$(sText, x, 1)
If IsNumeric(sActualChar) Then Only_Numbers = Only_Numbers & sActualChar
Next
End If
End FunctionOption Explicit
Option Base 0
Private Declare Function ArrayPtr Lib "msvbvm60" Alias "VarPtr" (ByRef Ptr() As Any) As Long
Private Declare Sub PutMem4 Lib "msvbvm60" (ByVal Ptr As Long, ByVal Value As Long)
Private Function GetNumbersByString(ByRef sText As String) As String
Dim intAsc() As Integer
Dim lngAscHeader(5) As Long
Dim lngPos As Long
Dim lngTextLen As Long
lngTextLen = LenB(sText) \ 2
If lngTextLen > 1 Then
lngAscHeader(0) = &H1
lngAscHeader(1) = &H2
lngAscHeader(3) = StrPtr(sText)
lngAscHeader(4) = lngTextLen
PutMem4 ArrayPtr(intAsc), VarPtr(lngAscHeader(0))
Do Until lngPos = lngTextLen
Do
If intAsc(lngPos) < 48 Then Exit Do
If intAsc(lngPos) > 57 Then Exit Do
GetNumbersByString = GetNumbersByString + CStr(intAsc(lngPos) - 48)
lngPos = lngPos + 1
Loop Until lngPos = lngTextLen
lngPos = lngPos + 1
Loop
PutMem4 ArrayPtr(intAsc), 0
End If
End FunctionPrivate Sub Form_Load()
Const s As String = "que 345 vivan 09453 las456 r4n4s"
Debug.Print GetNumbersByString(s)
End Sub3450945345644LL = "4234ABC45"
For i = 1 To Len(LL)
If IsNumeric(Mid(LL, i, 1)) Then
a = a & Mid(LL, i, 1)
End If
Next i
MsgBox (aa)
@wolf_kof
Probablemente esa sea la peor forma que hay... :-\
Él sabe que no lo dije a mal, somos buenos amigos, solo era una apreciación... :P@wolf_kof
Probablemente esa sea la peor forma que hay... :-\
No seas tan malo, lo que hizo es exactamente igual que la tuya, es lo que habrias pensado en forma de algoritmo y lo traspaso :P
Pero porque tanto codigo para lo que han pedido???
...
Mr.Frog -->4,785 msec
YvanB -->32,431 msec
Ya YvanB, solo ponía varias opciones... ;)
Tu forma es igual que la mia (versión clásica).
Gracias por el +1 ;D
DoEvents! :P
Option Explicit
Private Sub Form_Load()
MsgBox ExtraerNumeros("452FH3.34-F0")
MsgBox ExtraerNumeros2("452FH3.34-F0")
End Sub
Private Function ExtraerNumeros(ByVal sCadena As String) As String
Dim i As Long
Dim sChr As String
For i = 1 To Len(sCadena)
sChr = Mid$(sCadena, i, 1)
Select Case sChr
Case 0 To 9
ExtraerNumeros = ExtraerNumeros & sChr
End Select
Next
End Function
Private Function ExtraerNumeros2(ByVal sCadena As String) As String
Dim i As Long
Dim sChr As String
For i = 1 To Len(sCadena)
sChr = Mid$(sCadena, i, 1)
If IsNumeric(sChr) Then
ExtraerNumeros2 = ExtraerNumeros2 & sChr
End If
Next
End Function
huyyy cualquiera me mande, vi solo la respuesta de wolf_kof son iguales a las que ya pusieron.
Private Function LosNumeros(Cadena As String) As String
Dim c As Integer
For c = 1 To Len(Cadena)
If InStr("1234567890", Mid$(Cadena, c, 1)) Then LosNumeros = LosNumeros & Mid$(Cadena, c, 1)
Next
End Function
dulces besos
PD: ;D comprueven la velocidad (Mi Basura de Codigo) le gana a MrFrog
Public Class Form1
Private Function obtenerNumeros(pTexto As String)
Dim i As Integer = 0
Dim retorno As String = Nothing
While i < pTexto.Length
If pTexto(i) >= "0" And
pTexto(i) <= "9" Then
retorno += pTexto(i)
End If
i += 1
End While
Return retorno
End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
MsgBox(obtenerNumeros(TextBox1.Text))
End Sub
End Class
Gente este método no falla nunca y es lo más rápido que hay.Código: [Seleccionar]Public Class Form1
Private Function obtenerNumeros(pTexto As String)
Dim i As Integer = 0
Dim retorno As String = Nothing
While i < pTexto.Length
If pTexto(i) >= "0" And
pTexto(i) <= "9" Then
retorno += pTexto(i)
End If
i += 1
End While
Return retorno
End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
MsgBox(obtenerNumeros(TextBox1.Text))
End Sub
End Class