StringHelper.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Web;
  8. using Core.Util.Common;
  9. namespace Core.Util.Extension
  10. {
  11. public static class StringHelper
  12. {
  13. #region 正则表达式
  14. private static readonly Regex WebUrlExpression = new Regex(
  15. @"(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?", RegexOptions.Singleline | RegexOptions.Compiled);
  16. private static readonly Regex EmailExpression =
  17. new Regex(@"^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$",
  18. RegexOptions.Singleline | RegexOptions.Compiled);
  19. private static readonly Regex StripHtmlExpression = new Regex("<\\S[^><]*>",
  20. RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.CultureInvariant |
  21. RegexOptions.Compiled);
  22. private static readonly char[] IllegalUrlCharacters =
  23. {
  24. ';', '/', '\\', '?', ':', '@', '&', '=', '+', '$', ',',
  25. '<', '>', '#', '%', '.', '!', '*', '\'', '"', '(', ')', '[', ']', '{', '}', '|', '^', '`', '~', '–', '‘',
  26. '’', '“', '”', '»', '«'
  27. };
  28. #endregion
  29. #region 字符串判断
  30. public static bool IsNullOrEmpty(this string target)
  31. {
  32. return target == null || target.Length <= 0 || string.IsNullOrEmpty(target);
  33. }
  34. /// <summary>是否空或者空白字符串</summary>
  35. public static bool IsNullOrWhiteSpace(this string value)
  36. {
  37. return value == null || value.All(char.IsWhiteSpace);
  38. }
  39. /// <summary> 合法URL </summary>
  40. public static bool IsWebUrl(this string target)
  41. {
  42. return !target.IsNullOrEmpty() && WebUrlExpression.IsMatch(target);
  43. }
  44. /// <summary> 合法邮箱地址 </summary>
  45. public static bool IsEmail(this string target)
  46. {
  47. return !target.IsNullOrEmpty() && EmailExpression.IsMatch(target);
  48. }
  49. #endregion
  50. #region 格式转换
  51. /// <summary> 字符串转Hash </summary>
  52. public static string Hash(this string target)
  53. {
  54. Argument.IsNotEmpty(target, "target");
  55. using (var md5 = MD5.Create())
  56. {
  57. var data = Encoding.Unicode.GetBytes(target);
  58. var hash = md5.ComputeHash(data);
  59. return Convert.ToBase64String(hash);
  60. }
  61. }
  62. /// <summary>字符串转数组 默认UTF8</summary>
  63. public static byte[] GetBytes(this string target, Encoding encoding = null)
  64. {
  65. if (target == null) return null;
  66. if (target == string.Empty) return new byte[0];
  67. if (encoding == null) encoding = Encoding.UTF8;
  68. return encoding.GetBytes(target);
  69. }
  70. /// <summary> 转脚本 </summary>
  71. public static string StripHtml(this string target)
  72. {
  73. return StripHtmlExpression.Replace(target, string.Empty);
  74. }
  75. /// <summary> 转GUID </summary>
  76. public static Guid ToGuid(this string target)
  77. {
  78. var result = Guid.Empty;
  79. if (!string.IsNullOrEmpty(target) && (target.Trim().Length == 22))
  80. {
  81. var encoded = string.Concat(target.Trim().Replace("-", "+").Replace("_", "/"), "==");
  82. try
  83. {
  84. var base64 = Convert.FromBase64String(encoded);
  85. result = new Guid(base64);
  86. }
  87. catch (FormatException)
  88. {
  89. }
  90. }
  91. return result;
  92. }
  93. /// <summary> 字符串转枚举 </summary>
  94. public static T ToEnum<T>(this string target, T defaultValue) where T : IComparable, IFormattable
  95. {
  96. var convertedValue = defaultValue;
  97. if (!string.IsNullOrEmpty(target))
  98. {
  99. try
  100. {
  101. convertedValue = (T) Enum.Parse(typeof (T), target.Trim(), true);
  102. }
  103. catch (ArgumentException)
  104. {
  105. }
  106. }
  107. return convertedValue;
  108. }
  109. /// <summary> 合法URL </summary>
  110. public static string ToLegalUrl(this string target)
  111. {
  112. if (string.IsNullOrEmpty(target))
  113. {
  114. return target;
  115. }
  116. target = target.Trim();
  117. if (target.IndexOfAny(IllegalUrlCharacters) > -1)
  118. {
  119. foreach (var character in IllegalUrlCharacters)
  120. {
  121. target = target.Replace(character.ToString(Constants.CurrentCulture), string.Empty);
  122. }
  123. }
  124. target = target.Replace(" ", "-");
  125. while (target.Contains("--"))
  126. {
  127. target = target.Replace("--", "-");
  128. }
  129. return target;
  130. }
  131. /// <summary> 对URL字符串进行编码 </summary>
  132. public static string UrlEncode(this string target)
  133. {
  134. return target;// HttpUtility.UrlEncode(target);
  135. }
  136. /// <summary> 对URL字符串进行解码 </summary>
  137. public static string UrlDecode(this string target)
  138. {
  139. return target;// HttpUtility.UrlDecode(target);
  140. }
  141. /// <summary> 将字符串最小限度地转换为 HTML 编码的字符串。 </summary>
  142. public static string AttributeEncode(this string target)
  143. {
  144. return target;// HttpUtility.HtmlAttributeEncode(target);
  145. }
  146. /// <summary> 将字符串转换为 HTML 编码的字符串。 </summary>
  147. public static string HtmlEncode(this string target)
  148. {
  149. return target;// HttpUtility.HtmlEncode(target);
  150. }
  151. /// <summary> 将已经为 HTTP 传输进行过 HTML 编码的字符串转换为已解码的字符串。 </summary>
  152. public static string HtmlDecode(this string target)
  153. {
  154. return target;// HttpUtility.HtmlDecode(target);
  155. }
  156. #endregion
  157. #region 截取扩展
  158. /// <summary>
  159. /// 截断补.
  160. /// </summary>
  161. public static string WrapAt(this string target, int index)
  162. {
  163. const int dotCount = 3;
  164. Argument.IsNotEmpty(target, "target");
  165. Argument.IsNotNegativeOrZero(index, "index");
  166. return target.Length <= index
  167. ? target
  168. : string.Concat(target.Substring(0, index - dotCount), new string('.', dotCount));
  169. }
  170. /// <summary>确保字符串以指定的另一字符串开始,不区分大小写</summary>
  171. public static string EnsureStart(this string target, string start)
  172. {
  173. if (start.IsNullOrEmpty()) return target;
  174. if (target.IsNullOrEmpty()) return start;
  175. if (target.StartsWith(start, StringComparison.OrdinalIgnoreCase)) return target;
  176. return start + target;
  177. }
  178. /// <summary>确保字符串以指定的另一字符串结束,不区分大小写</summary>
  179. public static string EnsureEnd(this string target, string end)
  180. {
  181. if (end.IsNullOrEmpty()) return target;
  182. if (target.IsNullOrEmpty()) return end;
  183. if (target.EndsWith(end, StringComparison.OrdinalIgnoreCase)) return target;
  184. return target + end;
  185. }
  186. /// <summary>从当前字符串开头移除另一字符串,不区分大小写,循环多次匹配前缀</summary>
  187. public static string TrimStart(this string target, params string[] starts)
  188. {
  189. if (target.IsNullOrEmpty()) return target;
  190. if (starts == null || starts.Length < 1 || string.IsNullOrEmpty(starts[0])) return target;
  191. for (var i = 0; i < starts.Length; i++)
  192. {
  193. if (target.StartsWith(starts[i], StringComparison.OrdinalIgnoreCase))
  194. {
  195. target = target.Substring(starts[i].Length);
  196. if (string.IsNullOrEmpty(target)) break;
  197. // 从头开始
  198. i = -1;
  199. }
  200. }
  201. return target;
  202. }
  203. /// <summary>从当前字符串结尾移除另一字符串,不区分大小写,循环多次匹配后缀</summary>
  204. public static string TrimEnd(this string target, params string[] ends)
  205. {
  206. if (target.IsNullOrEmpty()) return target;
  207. if (ends == null || ends.Length < 1 || string.IsNullOrEmpty(ends[0])) return target;
  208. for (var i = 0; i < ends.Length; i++)
  209. {
  210. if (target.EndsWith(ends[i], StringComparison.OrdinalIgnoreCase))
  211. {
  212. target = target.Substring(0, target.Length - ends[i].Length);
  213. if (string.IsNullOrEmpty(target)) break;
  214. // 从头开始
  215. i = -1;
  216. }
  217. }
  218. return target;
  219. }
  220. /// <summary>从字符串中检索子字符串,在指定头部字符串之后,指定尾部字符串之前</summary>
  221. public static string Substring(this string target, string after, string before = null, int startIndex = 0,
  222. int[] positions = null)
  223. {
  224. if (target.IsNullOrEmpty()) return target;
  225. if (after.IsNullOrEmpty() && before.IsNullOrEmpty()) return target;
  226. var p = -1;
  227. if (!string.IsNullOrEmpty(after))
  228. {
  229. p = target.IndexOf(after, startIndex, StringComparison.Ordinal);
  230. if (p < 0) return null;
  231. p += after.Length;
  232. // 记录位置
  233. if (positions != null && positions.Length > 0) positions[0] = p;
  234. }
  235. if (string.IsNullOrEmpty(before)) return target.Substring(p);
  236. var f = target.IndexOf(before, p >= 0 ? p : startIndex, StringComparison.Ordinal);
  237. if (f < 0) return null;
  238. // 记录位置
  239. if (positions != null && positions.Length > 1) positions[1] = f;
  240. if (p >= 0)
  241. return target.Substring(p, f - p);
  242. return target.Substring(0, f);
  243. }
  244. /// <summary>根据最大长度截取字符串,并允许以指定空白填充末尾</summary>
  245. public static string Cut(this string str, int maxLength, string pad = null)
  246. {
  247. if (str.IsNullOrEmpty() || maxLength <= 0 || str.Length < maxLength) return str;
  248. // 计算截取长度
  249. var len = maxLength;
  250. if (!pad.IsNullOrEmpty()) len -= pad.Length;
  251. if (len <= 0) return pad;
  252. return str.Substring(0, len) + pad;
  253. }
  254. /// <summary>根据最大长度截取字符串(二进制计算长度),并允许以指定空白填充末尾</summary>
  255. /// <remarks>默认采用Default编码进行处理,其它编码请参考本函数代码另外实现</remarks>
  256. /// <param name="str">字符串</param>
  257. /// <param name="maxLength">截取后字符串的最大允许长度,包含后面填充</param>
  258. /// <param name="pad">需要填充在后面的字符串,比如几个圆点</param>
  259. /// <param name="strict">严格模式时,遇到截断位置位于一个字符中间时,忽略该字符,否则包括该字符。默认true</param>
  260. /// <returns></returns>
  261. public static string CutBinary(this string str, int maxLength, string pad = null, bool strict = true)
  262. {
  263. if (string.IsNullOrEmpty(str) || maxLength <= 0 || str.Length < maxLength) return str;
  264. var encoding = Encoding.Default;
  265. var buf = encoding.GetBytes(str);
  266. if (buf.Length < maxLength) return str;
  267. // 计算截取字节长度
  268. var len = maxLength;
  269. if (!string.IsNullOrEmpty(pad)) len -= encoding.GetByteCount(pad);
  270. if (len <= 0) return pad;
  271. // 计算截取字符长度。避免把一个字符劈开
  272. int clen;
  273. while (true)
  274. {
  275. try
  276. {
  277. clen = encoding.GetCharCount(buf, 0, len);
  278. break;
  279. }
  280. catch (DecoderFallbackException)
  281. {
  282. // 发生了回退,减少len再试
  283. len--;
  284. }
  285. }
  286. // 可能过长,修正
  287. if (strict) while (encoding.GetByteCount(str.ToCharArray(), 0, clen) > len) clen--;
  288. return str.Substring(0, clen) + pad;
  289. }
  290. /// <summary>从当前字符串开头移除另一字符串以及之前的部分</summary>
  291. /// <param name="str">当前字符串</param>
  292. /// <param name="starts">另一字符串</param>
  293. /// <returns></returns>
  294. public static string CutStart(this string str, params string[] starts)
  295. {
  296. if (string.IsNullOrEmpty(str)) return str;
  297. if (starts == null || starts.Length < 1 || string.IsNullOrEmpty(starts[0])) return str;
  298. for (var i = 0; i < starts.Length; i++)
  299. {
  300. var p = str.IndexOf(starts[i], StringComparison.Ordinal);
  301. if (p >= 0)
  302. {
  303. str = str.Substring(p + starts[i].Length);
  304. if (string.IsNullOrEmpty(str)) break;
  305. }
  306. }
  307. return str;
  308. }
  309. /// <summary>从当前字符串结尾移除另一字符串以及之后的部分</summary>
  310. /// <param name="str">当前字符串</param>
  311. /// <param name="ends">另一字符串</param>
  312. /// <returns></returns>
  313. public static string CutEnd(this string str, params string[] ends)
  314. {
  315. if (string.IsNullOrEmpty(str)) return str;
  316. if (ends == null || ends.Length < 1 || string.IsNullOrEmpty(ends[0])) return str;
  317. for (var i = 0; i < ends.Length; i++)
  318. {
  319. var p = str.LastIndexOf(ends[i], StringComparison.Ordinal);
  320. if (p >= 0)
  321. {
  322. str = str.Substring(0, p);
  323. if (string.IsNullOrEmpty(str)) break;
  324. }
  325. }
  326. return str;
  327. }
  328. #endregion
  329. #region 匹配查找
  330. /// <summary>忽略大小写的字符串相等比较,判断是否以任意一个待比较字符串相等</summary>
  331. public static bool EqualIgnoreCase(this string target, params string[] strs)
  332. {
  333. return !target.IsNullOrEmpty() &&
  334. strs.Any(item => string.Equals(target, item, StringComparison.OrdinalIgnoreCase));
  335. }
  336. /// <summary>忽略大小写的字符串开始比较,判断是否以任意一个待比较字符串开始</summary>
  337. public static bool StartsWithIgnoreCase(this string target, params string[] strs)
  338. {
  339. return !target.IsNullOrEmpty() &&
  340. strs.Any(item => target.StartsWith(item, StringComparison.OrdinalIgnoreCase));
  341. }
  342. /// <summary>忽略大小写的字符串结束比较,判断是否以任意一个待比较字符串结束</summary>
  343. public static bool EndsWithIgnoreCase(this string target, params string[] strs)
  344. {
  345. return !target.IsNullOrEmpty() &&
  346. strs.Any(item => target.EndsWith(item, StringComparison.OrdinalIgnoreCase));
  347. }
  348. public static int GetHashcode2(this string s)
  349. {
  350. if (string.IsNullOrEmpty(s)) return 0;
  351. unchecked
  352. {
  353. int hash = 23;
  354. foreach (char c in s)
  355. {
  356. hash = (hash << 5) - hash + c;
  357. }
  358. if (hash < 0)
  359. {
  360. hash = Math.Abs(hash);
  361. }
  362. return hash;
  363. }
  364. }
  365. #endregion
  366. #region 分隔
  367. /// <summary>拆分字符串,过滤空格,无效时返回空数组</summary>
  368. public static string[] Split(this string target, params string[] separators)
  369. {
  370. if (target.IsNullOrEmpty()) return new string[0];
  371. if (separators == null || separators.Length < 1 || separators.Length == 1 && separators[0].IsNullOrEmpty())
  372. separators = new[] {",", ";"};
  373. return target.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  374. }
  375. /// <summary>拆分字符串成为整型数组,默认逗号分号分隔,无效时返回空数组</summary>
  376. public static int[] SplitAsInt(this string target, params string[] separators)
  377. {
  378. if (target.IsNullOrEmpty()) return new int[0];
  379. if (separators == null || separators.Length < 1) separators = new[] {",", ";"};
  380. var ss = target.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  381. var list = new List<int>();
  382. foreach (var item in ss)
  383. {
  384. int id;
  385. if (!int.TryParse(item.Trim(), out id)) continue;
  386. list.Add(id);
  387. }
  388. return list.ToArray();
  389. }
  390. /// <summary>拆分字符串成为名值字典。逗号分号分组,等号分隔</summary>
  391. public static IDictionary<string, string> SplitAsDictionary(this string target, string nameValueSeparator = "=",
  392. params string[] separators)
  393. {
  394. var dic = new Dictionary<string, string>();
  395. if (target.IsNullOrWhiteSpace()) return dic;
  396. if (nameValueSeparator.IsNullOrEmpty()) nameValueSeparator = "=";
  397. if (separators == null || separators.Length < 1) separators = new[] {",", ";"};
  398. var ss = target.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  399. if (ss.Length < 1) return null;
  400. foreach (var item in ss)
  401. {
  402. var p = item.IndexOf(nameValueSeparator, StringComparison.Ordinal);
  403. // 在前后都不行
  404. if (p <= 0 || p >= item.Length - 1) continue;
  405. var key = item.Substring(0, p).Trim();
  406. dic[key] = item.Substring(p + nameValueSeparator.Length).Trim();
  407. }
  408. return dic;
  409. }
  410. #endregion
  411. #region 功能扩展
  412. /// <summary> 安全字符串 </summary>
  413. public static string NullSafe(this string target)
  414. {
  415. return (target ?? string.Empty).Trim();
  416. }
  417. /// <summary> 字符串格式化 </summary>
  418. public static string FormatWith(this string target, params object[] args)
  419. {
  420. Argument.IsNotEmpty(target, "target");
  421. for (var i = 0; i < args.Length; i++)
  422. {
  423. if (args[i] is DateTime)
  424. {
  425. if (target.Contains("{" + i + "}")) args[i] = ((DateTime) args[i]).ToFullString();
  426. }
  427. }
  428. return string.Format(target, args);
  429. }
  430. public static string Replace(this string target, ICollection<string> oldValues, string newValue)
  431. {
  432. oldValues.ForEach(oldValue => target = target.Replace(oldValue, newValue));
  433. return target;
  434. }
  435. #endregion
  436. #region 语音播放
  437. /// <summary>调用语音引擎说出指定话</summary>
  438. /// <param name="value"></param>
  439. public static void Speak(this string value)
  440. {
  441. //Speecher.Speak(value);
  442. }
  443. /// <summary>调用语音引擎说出指定话</summary>
  444. /// <param name="value"></param>
  445. public static void SpeakAsync(this string value)
  446. {
  447. //Speecher.SpeakAsync(value);
  448. }
  449. #endregion
  450. #region LD编辑距离算法
  451. /// <summary>编辑距离搜索,从词组中找到最接近关键字的若干匹配项</summary>
  452. /// <remarks>
  453. /// 算法代码由@Aimeast 独立完成。http://www.cnblogs.com/Aimeast/archive/2011/09/05/2167844.html
  454. /// </remarks>
  455. /// <param name="key">关键字</param>
  456. /// <param name="words">词组</param>
  457. /// <returns></returns>
  458. public static string[] LevenshteinSearch(string key, string[] words)
  459. {
  460. if (IsNullOrWhiteSpace(key)) return new string[0];
  461. var keys = key.Split(new[] {' ', ' '}, StringSplitOptions.RemoveEmptyEntries);
  462. foreach (var item in keys)
  463. {
  464. var maxDist = (item.Length - 1)/2;
  465. var q = from str in words
  466. where item.Length <= str.Length
  467. && Enumerable.Range(0, maxDist + 1)
  468. .Any(dist =>
  469. {
  470. return Enumerable.Range(0, Math.Max(str.Length - item.Length - dist + 1, 0))
  471. .Any(
  472. f =>
  473. {
  474. return LevenshteinDistance(item, str.Substring(f, item.Length + dist)) <=
  475. maxDist;
  476. });
  477. })
  478. orderby str
  479. select str;
  480. words = q.ToArray();
  481. }
  482. return words;
  483. }
  484. /// <summary>编辑距离</summary>
  485. /// <remarks>
  486. /// 又称Levenshtein距离(也叫做Edit Distance),是指两个字串之间,由一个转成另一个所需的最少编辑操作次数。
  487. /// 许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。
  488. /// 算法代码由@Aimeast 独立完成。http://www.cnblogs.com/Aimeast/archive/2011/09/05/2167844.html
  489. /// </remarks>
  490. /// <param name="str1"></param>
  491. /// <param name="str2"></param>
  492. /// <returns></returns>
  493. public static int LevenshteinDistance(string str1, string str2)
  494. {
  495. var n = str1.Length;
  496. var m = str2.Length;
  497. var C = new int[n + 1, m + 1];
  498. int i, j, x, y, z;
  499. for (i = 0; i <= n; i++)
  500. C[i, 0] = i;
  501. for (i = 1; i <= m; i++)
  502. C[0, i] = i;
  503. for (i = 0; i < n; i++)
  504. for (j = 0; j < m; j++)
  505. {
  506. x = C[i, j + 1] + 1;
  507. y = C[i + 1, j] + 1;
  508. if (str1[i] == str2[j])
  509. z = C[i, j];
  510. else
  511. z = C[i, j] + 1;
  512. C[i + 1, j + 1] = Math.Min(Math.Min(x, y), z);
  513. }
  514. return C[n, m];
  515. }
  516. #endregion
  517. #region LCS算法
  518. /// <summary>最长公共子序列搜索,从词组中找到最接近关键字的若干匹配项</summary>
  519. /// <remarks>
  520. /// 算法代码由@Aimeast 独立完成。http://www.cnblogs.com/Aimeast/archive/2011/09/05/2167844.html
  521. /// </remarks>
  522. /// <param name="key"></param>
  523. /// <param name="words"></param>
  524. /// <returns></returns>
  525. public static string[] LCSSearch(string key, string[] words)
  526. {
  527. if (IsNullOrWhiteSpace(key) || words == null || words.Length == 0) return new string[0];
  528. var keys = key
  529. .Split(new[] {' ', '\u3000'}, StringSplitOptions.RemoveEmptyEntries)
  530. .OrderBy(s => s.Length)
  531. .ToArray();
  532. //var q = from sentence in items.AsParallel()
  533. var q = from word in words
  534. let MLL = LCSDistance(word, keys)
  535. where MLL >= 0
  536. orderby (MLL + 0.5)/word.Length, word
  537. select word;
  538. return q.ToArray();
  539. }
  540. /// <summary>
  541. /// 最长公共子序列问题是寻找两个或多个已知数列最长的子序列。
  542. /// 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 S 称为已知序列的最长公共子序列。
  543. /// The longest common subsequence (LCS) problem is to find the longest subsequence common to all sequences in a set of
  544. /// sequences (often just two). Note that subsequence is different from a substring, see substring vs. subsequence. It
  545. /// is a classic computer science problem, the basis of diff (a file comparison program that outputs the differences
  546. /// between two files), and has applications in bioinformatics.
  547. /// </summary>
  548. /// <remarks>
  549. /// 算法代码由@Aimeast 独立完成。http://www.cnblogs.com/Aimeast/archive/2011/09/05/2167844.html
  550. /// </remarks>
  551. /// <param name="word"></param>
  552. /// <param name="keys">多个关键字。长度必须大于0,必须按照字符串长度升序排列。</param>
  553. /// <returns></returns>
  554. public static int LCSDistance(string word, string[] keys)
  555. {
  556. var sLength = word.Length;
  557. var result = sLength;
  558. var flags = new bool[sLength];
  559. var C = new int[sLength + 1, keys[keys.Length - 1].Length + 1];
  560. //int[,] C = new int[sLength + 1, words.Select(s => s.Length).Max() + 1];
  561. foreach (var key in keys)
  562. {
  563. var wLength = key.Length;
  564. int first = 0, last = 0;
  565. int i = 0, j = 0, LCS_L;
  566. //foreach 速度会有所提升,还可以加剪枝
  567. for (i = 0; i < sLength; i++)
  568. for (j = 0; j < wLength; j++)
  569. if (word[i] == key[j])
  570. {
  571. C[i + 1, j + 1] = C[i, j] + 1;
  572. if (first < C[i, j])
  573. {
  574. last = i;
  575. first = C[i, j];
  576. }
  577. }
  578. else
  579. C[i + 1, j + 1] = Math.Max(C[i, j + 1], C[i + 1, j]);
  580. LCS_L = C[i, j];
  581. if (LCS_L <= wLength >> 1)
  582. return -1;
  583. while (i > 0 && j > 0)
  584. {
  585. if (C[i - 1, j - 1] + 1 == C[i, j])
  586. {
  587. i--;
  588. j--;
  589. if (!flags[i])
  590. {
  591. flags[i] = true;
  592. result--;
  593. }
  594. first = i;
  595. }
  596. else if (C[i - 1, j] == C[i, j])
  597. i--;
  598. else // if (C[i, j - 1] == C[i, j])
  599. j--;
  600. }
  601. if (LCS_L <= (last - first + 1) >> 1)
  602. return -1;
  603. }
  604. return result;
  605. }
  606. #endregion
  607. #region 执行命令行
  608. ///// <summary>以隐藏窗口执行命令行</summary>
  609. ///// <param name="cmd">文件名</param>
  610. ///// <param name="arguments">命令参数</param>
  611. ///// <param name="msWait">等待毫秒数</param>
  612. ///// <param name="output">进程输出内容。默认为空时输出到日志</param>
  613. ///// <param name="onExit">进程退出时执行</param>
  614. ///// <returns>进程退出代码</returns>
  615. //public static int Run(this string cmd, string arguments = null, int msWait = 0, Action<string> output = null, Action<Process> onExit = null)
  616. //{
  617. // if (XTrace.Debug) XTrace.WriteLine("Run {0} {1} {2}", cmd, arguments, msWait);
  618. // var p = new Process();
  619. // var si = p.StartInfo;
  620. // si.FileName = cmd;
  621. // si.Arguments = arguments;
  622. // si.WindowStyle = ProcessWindowStyle.Hidden;
  623. // // 对于控制台项目,这里需要捕获输出
  624. // if (msWait > 0)
  625. // {
  626. // si.RedirectStandardOutput = true;
  627. // si.RedirectStandardError = true;
  628. // si.UseShellExecute = false;
  629. // if (output != null)
  630. // {
  631. // p.OutputDataReceived += (s, e) => output(e.Data);
  632. // p.ErrorDataReceived += (s, e) => output(e.Data);
  633. // }
  634. // else if (HouDa.Runtime.IsConsole)
  635. // {
  636. // p.OutputDataReceived += (s, e) => XTrace.WriteLine(e.Data);
  637. // p.ErrorDataReceived += (s, e) => XTrace.Current.Error(e.Data);
  638. // }
  639. // }
  640. // if (onExit != null) p.Exited += (s, e) => onExit(s as Process);
  641. // p.Start();
  642. // if (msWait > 0 && (output != null || HouDa.Runtime.IsConsole))
  643. // {
  644. // p.BeginOutputReadLine();
  645. // p.BeginErrorReadLine();
  646. // }
  647. // if (msWait <= 0) return -1;
  648. // // 如果未退出,则不能拿到退出代码
  649. // if (!p.WaitForExit(msWait)) return -1;
  650. // return p.ExitCode;
  651. //}
  652. #endregion
  653. }
  654. }