字符串是日常开发中最常见的基础类型之一,StringHelper内置了一些很有意思的小手段。

Nuget包:NewLife.Core

源码:https://github.com/NewLifeX/X/blob/master/NewLife.Core/Extension/StringHelper.cs

视频:https://www.bilibili.com/video/BV1gq4y1g78w

视频:https://www.bilibili.com/video/BV1uV4y1L7gG

空值判断IsNullOrEmpty/IsNullOrWhiteSpace

常用于链式写法,表达式得到字符串后马上判空

" abcd ".IsNullOrEmpty 等同于 String.IsNullOrEmpty(" abcd ")

" ".IsNullOrWhiteSpace 等同于 String.IsNullOrWhiteSpace(" ")


字符串比较EqualIgnoreCase

不区分大小写的字符串相等比较,其最大亮点在于要比较的目标字符串是一个数组,可以同时比较多个目标字符串是否相等

Boolean EqualIgnoreCase(this String? value, params String[] strs)


不区分大小写的头尾比较,支持比较数组

Boolean StartsWithIgnoreCase(this String? value, params String[] strs);
Boolean EndsWithIgnoreCase(this String? value, params String[] strs)


字符串拆分Split/SplitAsInt/SplitAsDictionary

字符串经常需要拆分为数组或字典

String[] Split(this String? value, params String[] separators); // 拆分为字符串数组
Int32[] SplitAsInt(this String? value, params String[] separators); // 拆分为整型数组
IDictionary<String, String> SplitAsDictionary(this String? value, String nameValueSeparator = "=", String separator = ";", Boolean trimQuotation = false); // 拆分为字典

需要注意的是,这里的Split,在最新netcore中也有类似版本


字符串组合Join

常用的字符串数组组合,默认逗号分隔

String Join<T>(this IEnumerable<T> value, String separator = ",", Func<T, Object>? func = null)


字符串转字节数组GetBytes

获取字符串的字节数组,默认使用Encoding.UTF8


字符串模糊匹配IsMatch

指定输入是否匹配目标表达式,支持*匹配

Boolean IsMatch(this String pattern, String input, StringComparison comparisonType = StringComparison.CurrentCulture)

例如 "*.newlifex.com".IsMatch("sso.newlifex.com") 就返回true。

字符串截取TrimStart/TrimEnd/Substring/Cut

抓取并解析数据时,经常需要用到字符串截取

  • EnsureStart,确保字符串以指定的另一字符串开始,若不是则添加,不区分大小写
  • EnsureEnd,确保字符串以指定的另一字符串结束,若不是则添加,不区分大小写
  • TrimStart,从当前字符串开头移除另一字符串,不区分大小写,循环多次匹配前缀
  • TrimEnd,从当前字符串结尾移除另一字符串,不区分大小写,循环多次匹配后缀
  • Substring,从字符串中检索子字符串,在指定头部字符串之后,指定尾部字符串之前
  • Cut,根据最大长度截取字符串,并允许以指定空白填充末尾


Substring很有意思,内置版本通过指定起始位和长度来实现截取,扩展版通过指定头尾字符串来截取,属于快速使用的扩展;

Cut扩展很简单,如果超过长度则截取前面一部分,确保长度不会越界


字符串模糊匹配搜索

有时候用户输入并非完整精确匹配目标,于是很需要模糊匹配。

算法研究编码由@Aimeast同学完成。

http://www.cnblogs.com/Aimeast/archive/2011/09/05/2167844.html


LCS最长公共子序列搜索,从词组中找到最接近关键字的若干匹配项

String[] LCSSearch(String key, String[] words)


LD编辑距离搜索,从词组中找到最接近关键字的若干匹配项

String[] LevenshteinSearch(String key, String[] words)


文本转语音Speak/SpeakAsync/SpeechTip

TTS仅用于Windows10,在Windows7上需要安装TTS运行时和语音包。

  • Speak,播放语音,等待播放完成才返回
  • SpeakAsync,异步播放语音,不需要等待完成即可返回,但多次异步调用的语音播放需要排队
  • SpeechTip,播放语音提示,受EnableSpeechTip开关控制
  • SpeakAsyncCancelAll,停止所有语音播放

示例:

"学无先后达者为师!".SpeakAsync();
"已连接服务器。".SpeechTip();
"拦截件 3 3 8 4".Speak();

Tips,想要播报几个数字,中间空格隔开,否则会当作一个整数报“三千三百八十四”。

注:net5.0/net6.0 中也可以使用该方法,最终项目从nuget引用包 System.Speech 。


执行命令Run

String.Run扩展将以隐藏窗口执行命令,并截取返回结果字符串。

方法原型:

Int32 Run(this String cmd, String? arguments = null, Int32 msWait = 0, Action<String>? output = null, Action<Process>? onExit = null)

各参数:

  • cmd,要执行的命令或文件名,如 "ping".Run("newlifex.com");
  • arguments,命令参数
  • msWait,等待退出的时间,0毫秒不等待,-1无限等待


外壳执行ShellExecute

String.ShellExecute扩展将用操作系统外壳执行命令。

方法原型:

Process ShellExecute(this String fileName, String? arguments = null, String? workingDirectory = null)