首页 > 解决方案 > 删除字符串之间的一些字符串

问题描述

我有一些字符串,比如

string word = "This is example text/WS95    1300 G934 100 DAB"

我想删除 WS95 和 G934 之间的字符串,结果将是:“这是示例文本/WS95 G934 100 DAB”

有什么办法吗?我尝试使用 indexof

int start = word.IndexOf("WS95") + "WS95".length;
int end = word.LastIndexOf("G");

在那之后,我被困住了。

也许有人在此之后有任何想法?

预期结果:“这是示例文本/WS95 G934 100 DAB”

谢谢

标签: c#

解决方案


试试这个..

string word = "This is example text/WS95    1300 G934 100 DAB";
var result = Regex.Replace(word, @"(?<=WS95).*(?= G934)","");

现场演示在这里

或者

string word = "This is example text/WS95    1300 G934 100 DAB";
var match = Regex.Matches(word, @"(.*WS95)(.*1300)(.*)")[0];
var result = match.Groups[1].Value+match.Groups[3].Value;

现场演示在这里


推荐阅读