首页 > 解决方案 > C# - 在文本之间取一个字符串

问题描述

我在列表中有各种字符串:

Ord.cl。N. 2724 删除 08/11/2019

它也可以

Ord.cl。N. 2725/web del 08/11/2019

我必须取“N”之后的所有内容。在'del'之前。结果我想要

有人可以在 C# 中为此编写代码吗?我知道有子字符串,但也许有更好的方法?

标签: c#stringsplitbetween

解决方案


你可以像这样构建一些扩展方法

    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() 如果你愿意


推荐阅读