linq - 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 -> 我将只提取第二列 ...
请帮我
解决方案
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() 。
推荐阅读
- html - SwiftUI 布局中的 WKWebViews 得到更新 - 变为空白
- mongodb - Mongoose:如何通过聚合框架将 $gte 和 $lte 用于数组
- modbus - SCADA-LTS 中的 Modbus TCP 问题
- javascript - 检查打字稿中数组中是否存在元素组合的最佳方法
- c# - 过滤器导航 每个导航只允许一个唯一的过滤器
- python - 使用 mysql 数据库转换 Python 文件
- gradle - 无法编译使用 gradle-swagger-generator-plugin 生成的代码
- python - 如何跟踪视频中检测到的特定人员?
- linux - 在交叉编译中链接二级依赖项
- python - 在 Pyspark 中获取 pandas Dataframe.to_numpy() 的另一种方法