You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#的List<string>中按标识查找对应静态字符串?

解决方案:区分文本类别来检索List中的对应字符串

这个问题的核心在于你当前的List<string>只存储了纯文本内容,没有保留「该文本属于描述类还是量级类」的元信息,直接用字符串值查找不仅逻辑不通(你添加的是DescriptionText1/2这类变量,但查找时用的是DescriptionText,两者根本不是同一个值),还没法区分不同类别的文本。下面给你几个实用的解决方案,按推荐程度排序:

1. 使用Dictionary<string, string>(最推荐)

用字典的键来标识文本类别(比如"Description"、"Magnitude"),值存储对应的文本内容,这样检索时直接通过键获取,逻辑清晰且效率高。

填充字典的代码:

// 初始化字典
Dictionary<string, string> textCollection = new Dictionary<string, string>();

// 根据条件添加描述文本
textCollection["Description"] = number < 2 ? DescriptionText1 : DescriptionText2;

// 根据条件添加量级文本
textCollection["Magnitude"] = (magnitude > 128 && number < 256) ? MagnitudeText1 : MagnitudeText2;

// 其他类别同理添加,比如textCollection["OtherType"] = ...

检索文本的方法:

public void Collection(Dictionary<string, string> ts) 
{
    // 直接通过键获取,也可以用TryGetValue避免键不存在的异常
    if (ts.TryGetValue("Description", out string description))
    {
        // 成功获取到描述文本,后续逻辑处理
    }
    
    if (ts.TryGetValue("Magnitude", out string magnitude))
    {
        // 成功获取到量级文本,后续逻辑处理
    }
}

2. 使用自定义类存储带类型的文本

如果你需要保留更多元信息,或者后续可能扩展文本的其他属性,自定义类是更好的选择。

定义类型和类:

// 枚举文本类别
public enum TextCategory
{
    Description,
    Magnitude
    // 可以添加更多类别,比如Other1, Other2等
}

// 存储带类型的文本
public class CategorizedText
{
    public TextCategory Category { get; set; }
    public string Content { get; set; }
}

填充列表的代码:

List<CategorizedText> textCollection = new List<CategorizedText>();

// 添加描述文本
textCollection.Add(new CategorizedText
{
    Category = TextCategory.Description,
    Content = number < 2 ? DescriptionText1 : DescriptionText2
});

// 添加量级文本
textCollection.Add(new CategorizedText
{
    Category = TextCategory.Magnitude,
    Content = (magnitude > 128 && number < 256) ? MagnitudeText1 : MagnitudeText2
});

检索文本的方法:

public void Collection(List<CategorizedText> ts) 
{
    // 根据类别查找对应的文本
    string description = ts.First(t => t.Category == TextCategory.Description).Content;
    string magnitude = ts.First(t => t.Category == TextCategory.Magnitude).Content;
    
    // 建议用FirstOrDefault避免找不到对应类别时抛出异常
    var descItem = ts.FirstOrDefault(t => t.Category == TextCategory.Description);
    if (descItem != null)
    {
        string descriptionSafe = descItem.Content;
    }
}

3. 给文本添加前缀标识(兼容现有List的方案)

如果一定要继续使用List<string>,可以给不同类别的文本添加唯一前缀,检索时通过前缀筛选并截取真实内容。

填充列表的代码:

List<string> stringCollection = new List<string>();

// 添加带前缀的描述文本
stringCollection.Add(number < 2 ? $"[DESC]{DescriptionText1}" : $"[DESC]{DescriptionText2}");

// 添加带前缀的量级文本
stringCollection.Add((magnitude > 128 && number < 256) ? $"[MAG]{MagnitudeText1}" : $"[MAG]{MagnitudeText2}");

检索文本的方法:

public void Collection(List<string> ts) 
{
    // 查找带[DESC]前缀的文本并截取内容
    string description = ts.First(s => s.StartsWith("[DESC]")).Substring("[DESC]".Length);
    // 查找带[MAG]前缀的文本并截取内容
    string magnitude = ts.First(s => s.StartsWith("[MAG]")).Substring("[MAG]".Length);
    
    // 同样建议用FirstOrDefault做安全处理
    var descStr = ts.FirstOrDefault(s => s.StartsWith("[DESC]"));
    if (descStr != null)
    {
        string descriptionSafe = descStr.Substring("[DESC]".Length);
    }
}

内容的提问来源于stack exchange,提问作者Jeppe Christensen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:22:44