我有一个List字符串,每个值都有一个需要删除的引号。现在可能会在字符串的下方引用,这些将需要保留。
List<string> strings = new List<string>();
strings.Add("'Value1");
strings.Add("'Values2 This 2nd ' should stay");
有没有linq方式?
答案 0 :(得分:4)
strings = strings.Select(x => x.StartsWith("'") ? x.Substring(1) : x).ToList();
答案 1 :(得分:3)
strings.Select(s => s.StartsWith("'") ? s.Substring(1) : s);
答案 2 :(得分:3)
var result = strings.Select(s => s.TrimStart('\''));
注意:这将删除(')的所有前导事件。但是,我假设您没有像"''Value1"
这样的字符串。
答案 3 :(得分:1)
LINQ对此非常不必要。您可以单独使用TrimStart():
strings.Add("'Value1".TrimStart('\''));
答案 4 :(得分:0)
strings.ForEach(s => s = s.TrimStart('\''));
Olivier Jacot-Descombes的编辑(它证明了这个解决方案不起作用):
List<string> strings = new List<string>();
strings.Add("'Value1");
strings.Add("'Values2 This 2nd ' should stay");
Console.WriteLine("Before:");
foreach (string s in strings) {
Console.WriteLine(s);
}
strings.ForEach(s => s = s.TrimStart('\''));
Console.WriteLine();
Console.WriteLine("After:");
foreach (string s in strings) {
Console.WriteLine(s);
}
Console.ReadKey();
这会在控制台上产生以下输出:
Before:
'Value1
'Values2 This 2nd ' should stay
After:
'Value1
'Values2 This 2nd ' should stay