【问题标题】:How can i use multiple delimiters in Array List如何在数组列表中使用多个分隔符
【发布时间】:2016-04-28 08:26:53
【问题描述】:

我想问你下面的代码:

 string[] seledCats = new string[0];

 string condsCats = EzCoding.Web.UI.QueryStringParsing.GetValue(
   "CondsCats",
   EzCoding.Web.RequestMethod.Post);

 if (condsCats != null)
 {
     seledCats = condsCats.Split(new string[] { "," },
                                 StringSplitOptions.RemoveEmptyEntries);
 }

在数组列表中插入选中的数据后,输出类似A1,A2, 但我想像这样展示它'A1','A2'

那么,我该怎么做呢? 谢谢。

【问题讨论】:

    标签: c# asp.net split delimiter


    【解决方案1】:

    您可以使用这个小 LINQ 查询:

    string condsCats = EzCoding.Web.UI.QueryStringParsing.GetValue("CondsCats",EzCoding.Web.RequestMethod.Post);
    string[] seledCats = null;
    if(condsCats != null)
        seledCats = condsCats 
            .Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
            .Select(s => String.Format("'{0}'", s))
            .ToArray();
    

    【讨论】:

    • 我认为var condsCatsstring 而不是string[]。他为什么要拆分数组?
    • 但是现在你把它放在 condsCats 中
    【解决方案2】:
    seledCats = condsCats.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
    string s = "'" + string.Join("','", seledCats) + "'";
    //to split into array again...
    seledCats = condsCats.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
    

    【讨论】:

    • 这不会添加一个前导,吗? :)
    • 不,join 在数组元素之间添加指定的字符串。
    • 是的,很好'String.Join()。不过,他并没有要求逗号后面的空格。
    • @Nyerguds 该空间是自动添加的 :)
    • @Nino 不,我的意思是在"', '" - 你在每个插入的逗号后面添加一个空格。
    猜你喜欢
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 2018-02-22
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    相关资源
    最近更新 更多