【问题标题】:how to extract those elements from the list which have square bracket in c# [closed]如何从列表中提取那些在c#中具有方括号的元素[关闭]
【发布时间】:2014-05-20 11:19:09
【问题描述】:

如何从列表中提取那些在c#中有方括号的元素

例如。

list<string> lst=new List<string>(){ "ABC","[ABC]", "AB[c", "AB]C","ab 1" "12a", "ab1","[abc 1]", "12","15","[XYZ-12ac]","ab 1", "[233]" };

从上面我想创建列表,比如..

AlphaNumeric_List= {"AB[c", "AB]C", "12a", "ab1", "ab 1"}
OuterBrackets_List={"[XYZ-12ac]", "[233]","[ABC]"}

【问题讨论】:

  • 你有没有尝试过?
  • "[abc" 会进入哪个列表? "[ac]c"呢?

标签: c# list


【解决方案1】:

试试这个

var OuterBrackets_List= lst.Where(s => s.StartsWith("[") && s.EndsWith("]");
var AlphaNumeric_List = lst.Except(OuterBrackets_List);

【讨论】:

  • @OP 可能足以满足您的要求,但此解决方案在 AlphaNumeric_List 中包含 numericonly alphabetic
【解决方案2】:
foreach(string s in lst)
{
   if(s.StartsWith("[") && s.EndsWith("]"))
   {
         //add to OuterBracket_List
         OuterBracket_List.Add(s);
   }
   else 
   {        
       int n;
       if (int.TryParse(s, out n) == false)
       {             
            //add AlphaNumeric_List
             AlphaNumeric_List.Add(s);
       }
       else
       {
            //add n to Numeric List
       }
   }
}

更新 1

上面的代码处理用括号 [] 括起来的字母数字、数字和字符串。

更新 2

可能这里的所有解决方案都包括数值,即AlphaNumeric_List 中的1215ABC

但也只有字母类别,但我已将您的列表区分为 3 类字符串:

  1. 字符串enclosed in brackets 例如。 “[ABC]”
  2. 字符串containing Alpha-numeric 例如。 "ab 1"
  3. 字符串 containing numeric characters 仅限 ex。 12

这里是更新的代码:

foreach (string s in lst)
{
    if (s.StartsWith("[") && s.EndsWith("]"))
    {
        //add to OuterBracket_List
        OuterBracket_List.Add(s);
    }
    else
    {
        int n;
        if (int.TryParse(s, out n) == false && isAlphaNumeric(s))                   
        {     
            //add AlphaNumeric_List
            AlphaNumeric_List.Add(s);
        } 
        else
        {
            //add n to Numeric List if required
        }
    }
}

//method to check string is AlphaNumeric Note: Regex can be used
public bool isAlphaNumeric(string strToCheck)
{            
    for (int i = 0; i < strToCheck.Length; i++)
    {
        if (char.IsLetter(strToCheck[i]) == false)
        {
            return true;
        }
    }
    return false;
}

这里是输入:

List<string> lst = new List<string>() { "ABC", "[ABC]", "AB[c", "AB]C", 
                                        "ab 1", "12a", "ab1", "[abc 1]", 
                                        "12", "15", "[XYZ-12ac]", "ab 1", 
                                        "[233]" };

这是输出:

OuterBracket_List = {"[ABC]", "[abc 1]", "[XYZ-12ac]", "[233]"}
AlphaNumeric_List = {"AB[c", "AB]C", "ab 1", "12a", "ab1", "ab 1"}

【讨论】:

  • 这可能是最不性感的,但它也是迄今为止发布的所有答案中性能最好的。为了简单起见,还有一个案例。
【解决方案3】:

使用 StartsWith\EndsWith(感谢 musefan):

AlphaNumeric_List = list.Where(s => !(s.StartsWith("[") && s.EndsWith("]"))).ToList();
OuterBrackets_List = list.Where(s => (s.StartsWith("[") && s.EndsWith("]"))).ToList();

【讨论】:

  • 我认为这是不对的,但我不会投反对票,因为 OP 问题并不完全 100% 明确.. 但您可能应该使用 EndsWith 作为右括号。另外,我的假设是它应该同时具有左括号和右括号才能被称为“外括号”。不过我可能是错的
  • @musefan - 他不够清楚 - 感谢您的评论。
【解决方案4】:

如果你愿意使用 LINQ,那就这样吧……

//get list of values that are NOT wrapped in square brackets
List<string> AlphaNumeric_List  - lst.Where(x => !x.StartsWith("[") || !x.EndsWith("]")).ToList();

//get list of values that ARE wrapped in square brackets
List<string> OuterBrackets_List = lst.Where(x => x.StartsWith("[") && x.EndsWith("]")).ToList();

【讨论】:

  • 你可能是对的,但他并没有具体说他只希望在开始或结束时删除带有括号的项目。
  • @MaxMommersteeg:当然他可能有其他要求,但这些是我可以从提供的示例输出中推断出的唯一规则
猜你喜欢
  • 2021-10-16
  • 1970-01-01
  • 2012-09-21
  • 1970-01-01
  • 1970-01-01
  • 2021-08-23
  • 1970-01-01
  • 2020-02-08
  • 2023-01-28
相关资源
最近更新 更多