//************************************************************************ // https://github.com/yuzhengyang // author: yuzhengyang // date: 2017.3.29 - 2017.8.16 // desc: 字符串工具类 // Copyright (c) yuzhengyang. All rights reserved. //************************************************************************ using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using Y.Utils.DataUtils.Collections; namespace Y.Utils.DataUtils.StringUtils { public sealed class StringTool { /// /// 判断字符串为null或为空格 /// /// /// public static bool IsNullOrWhiteSpace(string str) { if (str == null) return true; if (str.Trim().Length == 0) return true; return false; } /// /// 分割字符串 /// /// /// /// /// public static int Split(string str, char separator, out string[] result) { if (!string.IsNullOrWhiteSpace(str)) { string[] list = str.Split(separator); if (ListTool.HasElements(list)) { result = list; return result.Length; } } result = null; return 0; } /// /// 字符串中字符出现次数 /// /// /// /// public static int SubStringCount(string s, string sub) { if (s.Contains(sub)) { string sReplaced = s.Replace(sub, ""); return (s.Length - sReplaced.Length) / sub.Length; } return 0; } /// /// 根据通配符验证字符串 /// /// 字符串 /// 通配符:%和_ /// public static bool IsMatch(string s, string pattern) { try { //key = key.Replace("%", @"[\s\S]*").Replace("_", @"[\s\S]"); pattern = pattern.Replace("%", ".*").Replace("_", "."); return Regex.IsMatch(s, pattern); } catch { return false; } } } }