如何在字符串中添加点

时间:2012-09-06 05:14:28

标签: c# linq

我只想将所有这些查询作为单个查询运行。

string[] arrs = new string[] { ".sds", ".pps", "tbt", "dd." };

string[] val= (from string i in arrs
               where !i.StartsWith(".")
               select "."+i).ToArray();

string[] val1 = (from i in arrs
                 where i.StartsWith(".")
                 select i).ToArray();

var p = val1.Union(val);//get value in single array.e

2 个答案:

答案 0 :(得分:6)

var p = (from s in arrs
         select s.StartsWith(".") ? s : "." + s).ToArray();

或非查询语法:

var p = arrs.Select(s => s.StartsWith(".") ? s : "." + s)
            .ToArray();

如果您不想像.Distinct()那样需要重复项,则可能需要添加{ "foo", ".foo" }

var p = arrs.Select(s => s.StartsWith(".") ? s : "." + s)
            .Distinct()
            .ToArray();

答案 1 :(得分:0)

这样的东西
var pp = arrs.Select(x => x.StartsWith(".") ? x : "." + x);