C# 如果找到,则获取字符串";“字符串”;在名单上

C# 如果找到,则获取字符串";“字符串”;在名单上,c#,list,C#,List,如果我在列表中找到了char,我想得到完整的字符串 我的代码: List<string> mylist = new List<string>(); // My List Contains : 1-Cat , 2-Dog , 3-Wolf , 4-Mouse string text = ""; if (mylist.contains("3")) { text = ...//Get The Line That The "3" Is Founded In It

如果我在列表中找到了char,我想得到完整的字符串

我的代码:

List<string> mylist = new List<string>(); 
// My List Contains : 1-Cat , 2-Dog , 3-Wolf , 4-Mouse
string text = "";
if (mylist.contains("3")) 
{
    text = ...//Get The Line That The "3" Is Founded In It
    // In This Case text must be : 3-Wolf
}
List mylist=new List();
//我的名单包括:1只猫,2只狗,3只狼,4只老鼠
字符串文本=”;
如果(mylist.contains(“3”))
{
text=…//获取在其中建立“3”的行
//在这种情况下,文本必须为:3-1
}
谢谢,

您可以使用此代码

var text = mylist.FirstOrDefault(x => x.Contains("3"));
// note: text will be **null** if not found in the list
您可以使用此代码

var text = mylist.FirstOrDefault(x => x.Contains("3"));
// note: text will be **null** if not found in the list

应通过以下方式进行维修:

        List<string> mylist = new List<string>() { "1-Cat", "2-Dog", "3-Wolf", "4-Mouse" };
        // My List Contains : 1-Cat , 2-Dog , 3-Wolf , 4-Mouse
        string text = "";
        text = mylist.FirstOrDefault(x => x.Contains('3')); // text will be 3-Wolf. In case there is no strings in the list that contains 3 text will be null.

        // If you want do some other things if the list contains "3" you can do the following:
        if (!string.IsNullOrEmpty(text))
        {
            // Do your stuff here
        }
List mylist=newlist(){“1-Cat”、“2-Dog”、“3-Wolf”、“4-Mouse”};
//我的名单包括:1只猫,2只狗,3只狼,4只老鼠
字符串文本=”;
text=mylist.FirstOrDefault(x=>x.Contains('3'));//文本将是3-Wolf。如果列表中没有包含3个文本的字符串,则该字符串将为空。
//如果要执行其他操作,如果列表中包含“3”,则可以执行以下操作:
如果(!string.IsNullOrEmpty(text))
{
//在这里做你的事
}

以下各项应能完成此项服务:

        List<string> mylist = new List<string>() { "1-Cat", "2-Dog", "3-Wolf", "4-Mouse" };
        // My List Contains : 1-Cat , 2-Dog , 3-Wolf , 4-Mouse
        string text = "";
        text = mylist.FirstOrDefault(x => x.Contains('3')); // text will be 3-Wolf. In case there is no strings in the list that contains 3 text will be null.

        // If you want do some other things if the list contains "3" you can do the following:
        if (!string.IsNullOrEmpty(text))
        {
            // Do your stuff here
        }
List mylist=newlist(){“1-Cat”、“2-Dog”、“3-Wolf”、“4-Mouse”};
//我的名单包括:1只猫,2只狗,3只狼,4只老鼠
字符串文本=”;
text=mylist.FirstOrDefault(x=>x.Contains('3'));//文本将是3-Wolf。如果列表中没有包含3个文本的字符串,则该字符串将为空。
//如果要执行其他操作,如果列表中包含“3”,则可以执行以下操作:
如果(!string.IsNullOrEmpty(text))
{
//在这里做你的事
}

myList.First(p=>p==“3”)?myList.First(p=>p==“3”)?您的解决方案很好,但包含一些打字错误,从“3”到“3”,或者更确切地说是包含(“3”),因为OP要求
char
。非常感谢,有效:)@Ian:谢谢你指出错误
Contains(“3”)
更接近OP的示例代码在我看来,
var text=mylist.FirstOrDefault(x=>x.Contains(“3”)??字符串。空
更适合原始问题?@UweKeim:同意你的说法,它更接近OP的示例代码,但如果
未找到,我宁愿将其保持为
null
,而不是将其设置为空字符串。你的解决方案很好,但包含一些键入错误,包含“3”到包含(“3”)或包含('3'))由于OP要求提供
char
。非常感谢,有效:)@Ian:谢谢你指出错误
Contains(“3”)
更接近OP的示例代码在我看来,
var text=mylist.FirstOrDefault(x=>x.Contains(“3”)??字符串。空
更适合原始问题?@UweKeim:同意你的说法,它更接近OP的示例代码,但如果
未找到,我宁愿将其保持为
null
,而不是将其设置为空字符串