c# - C# - 在文本之间取一个字符串
问题描述
我在列表中有各种字符串:
Ord.cl。N. 2724 删除 08/11/2019
它也可以
Ord.cl。N. 2725/web del 08/11/2019
我必须取“N”之后的所有内容。在'del'之前。结果我想要
- 2724
- 2725/网络
有人可以在 C# 中为此编写代码吗?我知道有子字符串,但也许有更好的方法?
解决方案
你可以像这样构建一些扩展方法
public string SubstringFromTo(this string input, int from, int to)
{
return input.Substring(from, (to - from));
}
public string SubstringFromTo(this string input, string from, string to)
{
var index1 = input.IndexOf(from) != -1 ? input.IndexOf(from) : 0;
var index2 = input.IndexOf(to) != -1 ? input.IndexOf(to) : (input.Length - 1);
return input.SubstringFromTo(index1, index2);
}
var asd = " ciao ** come stai ? asdasd".SubstringFromTo("**","?");
result = "come stai"
//.Trim() 如果你愿意
推荐阅读
- date - 当我的日期切片器按一周过滤时,SAMEPERIODLASTYEAR 给我整个月的销售数据?
- visual-studio - Visual Studio - .Net 5.0 项目中的外部程序集未更新
- java - 是否有更快(更简洁)的方法来为 JDBC 中的 PreparedStatements 设置许多参数?
- python - 与所有文档作为本地 RAM 中的局部变量相比,带有 MongoDB 文档的 Python 迭代非常慢
- kotlin - kotlin 中是否有类似 joinToString for Int 的方法?
- getstream-io - 条件渲染和流的困难——使用自定义 MessageContent 或基于 message.customField 值的默认 MessageContent
- python - 将 Python 的 deepcopy 用于不同的 django 模型
- docker - Grafana docker /etc/grafana/grafana.ini 不可读
- next.js - Vercel Edge Functions 在本地工作,并在尝试使用“主机名重写”时在生产中给出 404
- python - 返回包含特定值的多索引 df 中的行