C# 截取或匹配字符串内包含指定字符

C# 截取或匹配字符串内包含指定字符
目录1. 使用正则表达式匹配并提取数字推荐场景 A提取“层”后面的数字如 “层305” - 305场景 B提取“寝”前面的数字如 “305寝” - 305场景 C通用提取同时兼容 “层305” 和 “305寝”2. 使用 Substring 和 IndexOf适用于格式极其固定的情况3. 完整示例代码关键点总结如果您喜欢此文章请收藏、点赞、评论谢谢祝您快乐每一天。C# 截取或匹配字符串内包含指定字符如层305、层101或305寝、101寝等其中数字是变化的。在 C# 中处理包含变化数字的字符串匹配或截取最灵活且推荐的方式是使用‌正则表达式Regular Expressions‌。此外如果格式非常固定也可以使用简单的字符串处理方法。以下是几种常见场景的解决方案1. 使用正则表达式匹配并提取数字推荐正则表达式可以精准地定位“层”或“寝”前后的数字无论数字是多少位。场景 A提取“层”后面的数字如 “层305” - 305using System;using System.Text.RegularExpressions;public class StringHelper{public static string ExtractNumberAfterLayer(string input){// 模式解释// 层 : 匹配 literal 字符 层// (\d) : 捕获组匹配一个或多个数字string pattern 层(\d);Match match Regex.Match(input, pattern);if (match.Success){return match.Groups[1].Value; // 返回捕获组中的数字部分}return null; // 未找到匹配}}场景 B提取“寝”前面的数字如 “305寝” - 305public static string ExtractNumberBeforeQin(string input){// 模式解释// (\d) : 捕获组匹配一个或多个数字// 寝 : 匹配 literal 字符 寝string pattern (\d)寝;Match match Regex.Match(input, pattern);if (match.Success){return match.Groups[1].Value;}return null;}场景 C通用提取同时兼容 “层305” 和 “305寝”如果你希望从同一个字符串中提取出那个核心数字不管它是在“层”后还是“寝”前public static string ExtractRoomNumber(string input){// 模式解释// 要么匹配 层 后面的数字要么匹配 寝 前面的数字string pattern (?:层)(\d)|(\d)(?:寝);Match match Regex.Match(input, pattern);if (match.Success){// Group[1] 对应 层(\d) 中的数字// Group[2] 对应 (\d)寝 中的数字if (!string.IsNullOrEmpty(match.Groups[1].Value))return match.Groups[1].Value;else if (!string.IsNullOrEmpty(match.Groups[2].Value))return match.Groups[2].Value;}return null;}2. 使用Substring和IndexOf适用于格式极其固定的情况如果字符串格式非常严格例如永远是“层XXX”或“XXX寝”可以使用传统字符串操作但容错性较差。public static string ExtractWithSubstring(string input){if (input.Contains(层)){int index input.IndexOf(层);// 假设数字紧跟在层之后且直到字符串结束或遇到非数字字符string remaining input.Substring(index 1);// 进一步提取纯数字部分return new string(remaining.TakeWhile(char.IsDigit).ToArray());}else if (input.Contains(寝)){int index input.IndexOf(寝);// 假设数字在寝之前string preceding input.Substring(0, index);// 从末尾向前提取纯数字部分return new string(preceding.Reverse().TakeWhile(char.IsDigit).Reverse().ToArray());}return null;}3. 完整示例代码using System;using System.Text.RegularExpressions;class Program{static void Main(){string[] testCases { 层305, 层101, 305寝, 101寝, A层305B, 房间305寝 };foreach (var test in testCases){string number ExtractRoomNumber(test);Console.WriteLine($输入: {test,-10} 提取数字: {number});}}/// summary/// 通用方法提取层后或寝前的数字/// /summarystatic string ExtractRoomNumber(string input){if (string.IsNullOrEmpty(input)) return null;// 尝试匹配 层 后面的数字Match matchLayer Regex.Match(input, 层(\d));if (matchLayer.Success){return matchLayer.Groups[1].Value;}// 尝试匹配 寝 前面的数字Match matchQin Regex.Match(input, (\d)寝);if (matchQin.Success){return matchQin.Groups[1].Value;}return null;}}‌输出结果输入: 层305 提取数字: 305输入: 层101 提取数字: 101输入: 305寝 提取数字: 305输入: 101寝 提取数字: 101输入: A层305B 提取数字: 305输入: 房间305寝 提取数字: 305关键点总结‌正则表达式\d‌用于匹配一个或多个连续数字是处理“变化数字”的核心。‌捕获组()‌用于只提取我们关心的数字部分而不是整个匹配字符串。‌Regex.Match‌用于查找第一个匹配项。如果需要查找所有匹配项如字符串中有多个房间号可使用Regex.Matches。‌灵活性‌正则方案能很好地处理前后有其他字符的情况如 “A层305B”而Substring方案则需要更复杂的逻辑来确保索引正确。如果您喜欢此文章请收藏、点赞、评论谢谢祝您快乐每一天。