替换VBA中的变量字符串 [英] Replace variable string in VBA
问题描述
我需要替换字符串中的某些内容,但是替换可能会有所不同。可以是
I need to replace somethin in a string, but what to replace may vary. It can be
XY - test
XXxY-test
XXyyXx-TEST
yXyy -Test
以及上述的任何其他组合的空白和案例。
and virtually any other combination of whitespaces and cases of the above.
我需要替换-test部分,单独离开XXX。所以,当使用简单的替换
I need to replace the "-test" part and leave the "XXX" alone. So, when using a simple replace
Replace("XXX -test", "- test", "")
显然不行。所以我需要一个更复杂的替代版本可以处理这个任务。有没有什么可以使用或者我必须自己写的?
It won't work, obviously. So I need a more sophisticated version of Replace that could handle this task. Is there something I could use or do I have to write it on my own?
推荐答案
如果你需要比所提供的更多的灵活性通过mj82的方法(例如,您可能不知道初始表达式的长度),可以使用正则表达式。例如:
If you need more flexibility than that provided by mj82's method (for example, you may not know the length of the initial expression), you can use a regular expression. For example:
Regex.Replace("XXX -test", "\s*-\s*test", "", RegexOptions.IgnoreCase)
这篇关于替换VBA中的变量字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!