String manipulation

  • Thread starter Thread starter Ray Batig
  • Start date Start date
R

Ray Batig

Greetings,

I have tried to write code to do this simple string manipulation, however, I
have not been successful. I guess I am just looking at it wrong.

An example string is Baby Blue 01/02/05 . 'Baby Blue' can be various words
and also there can be more. What I need to do is strip away the date
characters so I can change them. I can handle everything except stripping
away the existing date characters. How is this programmed?


Thanks in advance for your help!!
 
Hi Ray,

See if this makes sense...
'--------------------------------
Sub Test()
Dim strText As String
Dim strDate As String
Dim lngN As Long

strText = "Baby Blue 01/02/05"

'Find the first number
For lngN = 1 To Len(strText)
If Mid$(strText, lngN, 1) Like "#" Then
strDate = Mid$(strText, lngN, 99)
Exit For
End If
Next 'lngN

'do something to strDate if a number was found.
If Len(strDate) Then
strDate = "03/16/2005"
' Put it back together.
strText = Left$(strText, lngN - 1) & strDate
End If
MsgBox strText
End Sub
'-----------------------

Regards,
Jim Cone
San Francisco, USA
 
Function FindDate(ByVal TextIn As String) As String
Static regex As Object, matches As Object
Dim x As Integer
Dim sRet As String

If regex Is Nothing Then
Set regex = CreateObject("vbscript.regexp")
regex.Pattern = "[0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4}"
regex.Global = True
regex.ignorecase = True
End If

sRet = ""
Set matches = regex.Execute(TextIn)
If matches.Count > 0 Then
For x = 0 To matches.Count - 1
sRet = sRet & IIf(sRet = "", "", ",") & matches(x).Value
Next x
End If

FindDate = sRet

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top