C#截取或匹配字符串内包含指定字符的做法实用指南
平时做技术实践时,很多问题不是概念不会,而是细节没串起来。拿“C#截取或匹配字符串内包含指定字符的做法”来说,它看着像小点,放到项目里常会牵出环境、配置、兼容性和维护成本。下面按实际采用顺序,把思路、关键写法和容易踩坑的地方讲清楚,便于大家直接对照操作。
理解这一步时,C# 截取或匹配字符串内包含指定字符,如:层305、层101或305寝、101寝等,其中数字是变化的。
实际处理时,在 C# 里,处理包含变化数字的字符串匹配或截取,最灵活且建议的方式是采用 正则表达式(Regular Expressions)。此外,如果格式很固定,也能够采用轻松的字符串处理方法。
以下是几种常用场景的解决方案:
1. 采用正则表达式匹配并提取数字(建议)
正则表达式能够精准地定位“层”或“寝”前后的数字,无论数字是多少位。
场景 A:提取“层”后面的数字(如 “层305” -> 305)
using 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寝” -> 305)
public 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>
/// 通用方法:提取"层"后或"寝"前的数字
/// </summary>
static 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方案则需更复杂的逻辑来确保索引正确。
在这个场景下,总的来说,C#截取或匹配字符串指定字符适合结合实际项目边做边理解。先抓住核心思路,再逐步补上细节和边界处理,最后效果会更稳定,也更容易复用。
游戏推荐
推荐专题
热门阅读
-
- Git Stash贮藏命令使用及说明实用指南
- 10.24
-
- C#截取或匹配字符串内包含指定字符的做法实用指南
- 10.24
推荐下载
-
-
下载
- |
-
-
下载
- 《行尸走肉第一章》免安装中文汉化硬盘版下载
- 单机|436 MB
- 一款以动作冒险为主题的游戏
-
-
下载
- 《街头霸王X铁拳》免安装中文汉化硬盘版下载
- 单机|111MB
- 一款非常好玩的格斗游戏
-
-
下载
- |
-
-
下载
- 《暗黑破坏神3》免安装繁体中文正式版下载
- 单机|7630 MB
- 一款以角色扮演为主题的游戏
-
-
下载
- 《马克思佩恩3》免安装硬盘版下载
- 单机|27033 MB
- 一款以第三人称射击为主题的游戏