Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
I need a function to replace the string with date, If a string contains ##Date:[Number]##, then it should replace the Date:[Number] with todays date + Number

for example if the string contains ##Date:4##, and today is '19/12/2014' then it should replace with date '23/12/2014'.

thanks
Posted

Use a regex:
VB
Public Dim regex As Regex = New Regex( "\#\#Date:4\#\#", _
    RegexOptions.Multiline _
    Or RegexOptions.CultureInvariant _
    Or RegexOptions.IgnorePatternWhitespace _
    Or RegexOptions.Compiled _
    )


Dim result As String = regex.Replace(InputText,DateTime.Now.ToShortDateString())


"when I use Dim result As String = regex.Replace("some texts ##Date:4## some text ##Date:10##", DateTime.Now.ToShortDateString()) the output is 'some texts 12/19/2014 some text 12/19/2014' Note the date are same even if I use different number (4 and 10)"

You still use a Regex, but it needs a small mod to extract the "number" info into a separate group, parse that, and use it for the substitution. Only thing is, because they are different dates, you need a loop or a replace evaluator (which is a lot harder to get your brain round: Advanced Regex.Replace handling[^])
The loop version is pretty simple:
VB
Dim input As String = "some texts ##Date:4## some text ##Date:10##"
Dim forwardDate As New Regex("\#\#Date:(?<DaysCount>\d+)\#\#")
Dim output As String = input
Dim now As DateTime = DateTime.Now
For Each m As Match In forwardDate.Matches(input)
    Dim days As Integer = Integer.Parse(m.Groups("DaysCount").Value)
    output = Regex.Replace(output, m.Value, now.AddDays(days).ToShortDateString())
Next
 
Share this answer
 
v2
Comments
Jijutj 19-Dec-14 3:41am    
thank you, but I put 4 for an example, the number may change for example if I put ##Date:10## then it should add 10 to the current date. thanks
OriginalGriff 19-Dec-14 4:13am    
Yes? So use "\d" instead of "4" - it matches any single digit.
Or "\d+" if you want "one or more digit"
Jijutj 19-Dec-14 4:22am    
Thank you, it works when there is only one match, what if more than one match with different number?
OriginalGriff 19-Dec-14 4:29am    
Sorry, I don't understand what you are asking? Can you explain in better detail, perhaps with an example?
Jijutj 19-Dec-14 4:35am    
for example if I write the string as "some texts ##Date:4## some text ##Date:10##" then how to replace the string? Currently it will add 4 in both tokens
You should use Regex[^] for this task. Something like
VB
Dim s As String = "Fooo##Date:4##Goo123"
Dim match As Match = Regex.Match(s, "##Date:(\d+)##")
Dim n As Integer
n = Int32.Parse(match.Groups(1).Value)
Dim now As Date = Date.Now
now.AddDays(n)
s = Regex.Replace(s, "##Date:\d+##", now.ToString("d"))

with proper error checking.
 
Share this answer
 
Comments
Jijutj 19-Dec-14 4:06am    
what if the string contains more than one ##Date:[Number]##, thanks
VB
Dim lstrdate As String = "##Date:10##"
Dim lstrtemp As String
Dim i As Integer
Dim ldays As Integer

For i = 1 To Len(lstrdate)
    If InStr("0123456789", Mid(lstrdate, i, 1)) > 0 Then
        lstrtemp = lstrtemp & Mid(lstrdate, i, 1)
    End If
Next

ldays = Val(lstrtemp)
lstrtemp = Now.AddDays(ldays).ToShortDateString

MsgBox(lstrtemp)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900