找出字符串列表项是否以另一个列表中的另一个项开头 [英] Find out if string list items startswith another item from another list

查看:90
本文介绍了找出字符串列表项是否以另一个列表中的另一个项开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历一个字符串列表,并找出该列表中的项是否以另一个列表中的项开头.

I'd like to loop over a string list, and find out if the items from this list start with one of the item from another list.

所以我有类似的东西:

List<string> firstList = new List<string>();
firstList.Add("txt random");
firstList.Add("text ok");
List<string> keyWords = new List<string>();
keyWords.Add("txt");
keyWords.Add("Text");

推荐答案

对于每个循环,您可以使用几个简单的方法来做到这一点.

You can do that using a couple simple for each loops.

foreach (var t in firstList) {
    foreach (var u in keyWords) {
        if (t.StartsWith(u) {
            // Do something here.
        }
    }
}

这篇关于找出字符串列表项是否以另一个列表中的另一个项开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆