自定义格式字符串 [英] Custom Format String
问题描述
快速问题,我想实现一种自定义格式字符串,例如日期/时间对象.基本上,我只需要它来找到%P并将其替换为某些内容.不过,如果我可以轻松地将其扩展为其他功能,那就太好了.我在MSDN上找到了一篇文章,但并未对此发表任何评论.因此,如果有人可以向我提供样本或为我指明正确的方向,我将为您提供帮助!
谢谢!
M
Quick Question, I have something that I would like to implement a custom format string on like the Date/Time Object one. Basically, I just need it to find %P and replace it with something. Although, it would be great if I could easily expand it for other things. I found an article on MSDN but, I didn''t make heads or tales of it. So if anyone could provide me with a sample or point me in the right direction, I''d ''preciate it!
Thanks!
M
推荐答案
使用正则表达式:
Use a Regex:
Public Dim regex As Regex = New Regex( _
"(%P)", _
RegexOptions.IgnoreCase _
Or RegexOptions.CultureInvariant _
Or RegexOptions.IgnorePatternWhitespace _
Or RegexOptions.Compiled _
)
Public Dim regexReplace As String = _
"percentP"
Dim result As String = regex.Replace(InputText,regexReplace)
看看 Expresso [ ^ ]-创建,检查和解释正则表达式字符串.而且它是免费的.
Have a look at Expresso [^]- creates, examines and explains regex strings. And it''s free.
虽然Griff的解决方案是合适的,但我不是Regex的忠实拥护者,所以我会这样处理:
While Griff''s solution is appropriate, I''m not a big fan of Regex, so I would approach it this way:
myString = myString.Replace("%P", replacementString);
那是很多代码,比Regex快很多,并且消耗更少的资源.
That''s a LOT less code, a LOT faster than Regex will ever be, and consumes fewer resources besides.
这篇关于自定义格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!