首页 > 解决方案 > LINQ:选择字符串的任何单词以某个字符开头的行

问题描述

我想从表中提取列(字符串)中至少有一个以指定字符开头的单词的所有行。例子:

Row 1: 'this is the first row'
Row 2: 'this is th second row'
Row 3: 'this is the third row'

如果指定的字符是 T -> 我将提取所有 3 行如果指定的字符是 S -> 我将只提取第二列 ...

请帮我

标签: linq

解决方案


var yourChar = "s";

var texts = new List<string> {
    "this is the first row",
    "this is th second row",
    "this is the third row"
};

var result = texts.Where(p => p.StartsWith(yourChar) || p.Contains(" " + yourChar));

编辑:

替代方式(我不确定它是否适用于 linq 查询)

var result = texts.Where(p => (" " + p).Contains(" " + yourChar));
  • 如果你想要不区分大小写的检查,你可以使用 .ToLower() 。

推荐阅读