用于替换字符串的函数 [英] Function to replace the string

查看:80
本文介绍了用于替换字符串的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi


我需要一个函数来用日期替换字符串,如果一个字符串包含## Date:[Number] ##,那么它应该替换Date:[Number ]今天的日期+数字



例如,如果字符串包含## Date:4 ##,今天是'19 / 12/2014'那么它应该替换日期'23 / 12/2014'。



谢谢

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

推荐答案

使用正则表达式:

Use a regex:
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())





当我使用Dim result As String = regex.Replace(some some ## Date:4 ## some text ## Date:10 ##,DateTime.Now.ToShortDateString())输出为'一些文本2014年12月19日一些文本12/19/2014'注意即使我使用不同的数字(4和10),日期也是一样的



您仍然使用正则表达式,但它需要一个小的mod来将数字信息提取到一个单独的组中,解析它,并将其用于替换。唯一的问题是,因为它们是不同的日期,所以你需要一个循环或替换评估器(让你的大脑变得更加困难:高级Regex.Replace处理 [ ^ ])

循环版本非常简单:



"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:

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


你应该使用正则表达式 [ ^ ]。类似

You should use Regex[^] for this task. Something like
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.


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)


这篇关于用于替换字符串的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆