c# - 删除字符串之间的一些字符串
问题描述
我有一些字符串,比如
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”
谢谢
解决方案
试试这个..
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;
推荐阅读
- powershell - 根据 Get-AdUser 的结果设置 AD 用户的 UPN
- c - 返回 char 数组的 AC 函数与使用 2 个 char 数组的函数
- c# - CLR如何在程序集外调用内部类的Main()方法?
- jquery - 点击保存,如果出现提示前的条件,则显示确认消息
- java - java:符号查找错误:/lib64/libnssutil3.so:未定义符号:Oracle Linux 7.5 中的 PR_GetEnvSecure
- struts2 - Struts2 拦截器转换错误
- ios - Ambiguos 引用成员下标错误 swift 3
- c# - 如何以编程方式将“上次使用的文档”添加到 WPF 程序托盘符号(不是图标)?
- java - jaxb:防止创建冗余集合?
- eclipse - What's the project name of Eclipse 4.9?