Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在集合的Forloop中查找集合中字符串模式的索引_C#_Linq - Fatal编程技术网

C# 如何在集合的Forloop中查找集合中字符串模式的索引

C# 如何在集合的Forloop中查找集合中字符串模式的索引,c#,linq,C#,Linq,在循环遍历一个集合时,我检查“billingaddress”模式,然后希望读取并捕获所有行,直到“Shipping Address”的索引位置。 当在同一集合的for循环中时,如何在集合中查找string_模式的索引位置。 有没有一种有效的方法来做到这一点-也许使用Linq? 下面是一个示例代码剪贴- 提前谢谢 static void Main(string[] args) { string[] lines = new string[]

在循环遍历一个集合时,我检查“billingaddress”模式,然后希望读取并捕获所有行,直到“Shipping Address”的索引位置。 当在同一集合的for循环中时,如何在集合中查找string_模式的索引位置。 有没有一种有效的方法来做到这一点-也许使用Linq? 下面是一个示例代码剪贴- 提前谢谢

    static void Main(string[] args)
    {
        string[] lines = new string[] 
                                {
                                    "bla bla",
                                    "bla bla bla",
                                    "bla bla ...",
                                    "bla bla bla bla",
                                    "Billing Address",
                                    "Billing Address line 1",
                                    "Billing Address line 2",
                                    "Billing Address line 3",
                                    "Billing Address line 4",
                                    "Shipping Address",
                                    "Shipping Address line 1",
                                    "Shipping Address line 2",
                                    "Shipping Address line 3",
                                    "Shipping Address line 4",
                                };

        for (int i = 0; i < lines.Length; i++ )
        {
            if (lines[i].Contains("Billing Address"))
            {
                // Capture all the lines until the line "Shipping Address"

                // Is there a way maybe using Linq to get the index position of "Shipping Address"
            }
        }
    }
static void Main(字符串[]args)
{
字符串[]行=新字符串[]
{
“布拉布拉”,
“呜呜呜呜”,
“呜呜呜呜……”,
“呜呜呜呜”,
“账单地址”,
“账单地址第1行”,
“账单地址第2行”,
“账单地址第3行”,
“账单地址第4行”,
“送货地址”,
“发货地址行1”,
“送货地址行2”,
“送货地址第3行”,
“送货地址第4行”,
};
对于(int i=0;i
使用LINQ获取
行中
装运地址
索引的简单解决方案

var index = Enumerable.Range(0,lines.Length)
                       .FirstOrDefault(i => lines[i] == "Shipping Address");
或者您可以使用
Array.IndexOf
像@MongZhu在评论中建议的那样:

var index = Array.IndexOf(lines, "Shipping Address");
您可以捕获
中包含
“账单地址”
的所有元素,包括:

var billingLines = lines.Where(x => x.Contains("Billing Address")).ToArray();

您可以使用linq筛选集合:

var result = lines.Where(s => s.StartsWith("Billing Address"));
或者,如果您喜欢使用包含:

var result = lines.Where(s => s.Contains("Billing Address"));

关于林克我什么也说不出来。不是我的专长领域。但是如果你做了一个枚举来跟踪你所在的地址/模式(Bla、BillingAddress、ShippingAddress)的哪一部分,它就变成了一个简单的开关,并在解析行时设置枚举变量。那么预期的结果是什么呢?我认为linq在这里没有帮助。您只需要在for之前声明一个int,它将保存索引。当达到条件时,可以指定值。顺便说一句,如果你想要更多的oo-aproach,那么从使用列表而不是数组开始,它会对你有所帮助。IndexOf方法怎么样?这应该如何得到索引?