本篇文章給大家分享的是有關(guān)如何使用字符操作普通幫助類,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比棗強(qiáng)網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式棗強(qiáng)網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋棗強(qiáng)地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。本篇文章給大家?guī)?lái)的內(nèi)容是介紹字符如何操作普通幫助類?字符操作普通幫助類的方法(代碼示例)。有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)你們有所幫助。
////// 普通幫助類 /// public class CommonHelper { //星期數(shù)組 private static string[] _weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; //空格、回車、換行符、制表符正則表達(dá)式 private static Regex _tbbrRegex = new Regex(@"\s*|\t|\r|\n", RegexOptions.IgnoreCase); #region 時(shí)間操作 ////// 獲得當(dāng)前時(shí)間的""yyyy-MM-dd HH:mm:ss:fffffff""格式字符串 /// public static string GetDateTimeMS() { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffffff"); } ////// 獲得當(dāng)前時(shí)間的""yyyy年MM月dd日 HH:mm:ss""格式字符串 /// public static string GetDateTimeU() { return string.Format("{0:U}", DateTime.Now); } ////// 獲得當(dāng)前時(shí)間的""yyyy-MM-dd HH:mm:ss""格式字符串 /// public static string GetDateTime() { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } ////// 獲得當(dāng)前日期 /// public static string GetDate() { return DateTime.Now.ToString("yyyy-MM-dd"); } ////// 獲得中文當(dāng)前日期 /// public static string GetChineseDate() { return DateTime.Now.ToString("yyyy月MM日dd"); } ////// 獲得當(dāng)前時(shí)間(不含日期部分) /// public static string GetTime() { return DateTime.Now.ToString("HH:mm:ss"); } ////// 獲得當(dāng)前小時(shí) /// public static string GetHour() { return DateTime.Now.Hour.ToString("00"); } ////// 獲得當(dāng)前天 /// public static string GetDay() { return DateTime.Now.Day.ToString("00"); } ////// 獲得當(dāng)前月 /// public static string GetMonth() { return DateTime.Now.Month.ToString("00"); } ////// 獲得當(dāng)前年 /// public static string GetYear() { return DateTime.Now.Year.ToString(); } ////// 獲得當(dāng)前星期(數(shù)字) /// public static string GetDayOfWeek() { return ((int)DateTime.Now.DayOfWeek).ToString(); } ////// 獲得當(dāng)前星期(漢字) /// public static string GetWeek() { return _weekdays[(int)DateTime.Now.DayOfWeek]; } #endregion #region 數(shù)組操作 ////// 獲得字符串在字符串?dāng)?shù)組中的位置 /// public static int GetIndexInArray(string s, string[] array, bool ignoreCase) { if (string.IsNullOrEmpty(s) || array == null || array.Length == 0) return -1; int index = 0; string temp = null; if (ignoreCase) s = s.ToLower(); foreach (string item in array) { if (ignoreCase) temp = item.ToLower(); else temp = item; if (s == temp) return index; else index++; } return -1; } ////// 獲得字符串在字符串?dāng)?shù)組中的位置 /// public static int GetIndexInArray(string s, string[] array) { return GetIndexInArray(s, array, false); } ////// 判斷字符串是否在字符串?dāng)?shù)組中 /// public static bool IsInArray(string s, string[] array, bool ignoreCase) { return GetIndexInArray(s, array, ignoreCase) > -1; } ////// 判斷字符串是否在字符串?dāng)?shù)組中 /// public static bool IsInArray(string s, string[] array) { return IsInArray(s, array, false); } ////// 判斷字符串是否在字符串中 /// public static bool IsInArray(string s, string array, string splitStr, bool ignoreCase) { return IsInArray(s, StringHelper.SplitString(array, splitStr), ignoreCase); } ////// 判斷字符串是否在字符串中 /// public static bool IsInArray(string s, string array, string splitStr) { return IsInArray(s, StringHelper.SplitString(array, splitStr), false); } ////// 判斷字符串是否在字符串中 /// public static bool IsInArray(string s, string array) { return IsInArray(s, StringHelper.SplitString(array, ","), false); } ////// 將對(duì)象數(shù)組拼接成字符串 /// public static string ObjectArrayToString(object[] array, string splitStr) { if (array == null || array.Length == 0) return ""; StringBuilder result = new StringBuilder(); for (int i = 0; i < array.Length; i++) result.AppendFormat("{0}{1}", array[i], splitStr); return result.Remove(result.Length - splitStr.Length, splitStr.Length).ToString(); } ////// 將對(duì)象數(shù)組拼接成字符串 /// public static string ObjectArrayToString(object[] array) { return ObjectArrayToString(array, ","); } ////// 將字符串?dāng)?shù)組拼接成字符串 /// public static string StringArrayToString(string[] array, string splitStr) { return ObjectArrayToString(array, splitStr); } ////// 將字符串?dāng)?shù)組拼接成字符串 /// public static string StringArrayToString(string[] array) { return StringArrayToString(array, ","); } ////// 將整數(shù)數(shù)組拼接成字符串 /// public static string IntArrayToString(int[] array, string splitStr) { if (array == null || array.Length == 0) return ""; StringBuilder result = new StringBuilder(); for (int i = 0; i < array.Length; i++) result.AppendFormat("{0}{1}", array[i], splitStr); return result.Remove(result.Length - splitStr.Length, splitStr.Length).ToString(); } ////// 將整數(shù)數(shù)組拼接成字符串 /// public static string IntArrayToString(int[] array) { return IntArrayToString(array, ","); } ////// 移除數(shù)組中的指定項(xiàng) /// /// 源數(shù)組 /// 要移除的項(xiàng) /// 是否移除空格 /// 是否忽略大小寫 ///public static string[] RemoveArrayItem(string[] array, string removeItem, bool removeBackspace, bool ignoreCase) { if (array != null && array.Length > 0) { StringBuilder arrayStr = new StringBuilder(); if (ignoreCase) removeItem = removeItem.ToLower(); string temp = ""; foreach (string item in array) { if (ignoreCase) temp = item.ToLower(); else temp = item; if (temp != removeItem) arrayStr.AppendFormat("{0}_", removeBackspace ? item.Trim() : item); } return StringHelper.SplitString(arrayStr.Remove(arrayStr.Length - 1, 1).ToString(), "_"); } return array; } /// /// 移除數(shù)組中的指定項(xiàng) /// /// 源數(shù)組 ///public static string[] RemoveArrayItem(string[] array) { return RemoveArrayItem(array, "", true, false); } /// /// 移除字符串中的指定項(xiàng) /// /// 源字符串 /// 分割字符串 ///public static string[] RemoveStringItem(string s, string splitStr) { return RemoveArrayItem(StringHelper.SplitString(s, splitStr), "", true, false); } /// /// 移除字符串中的指定項(xiàng) /// /// 源字符串 ///public static string[] RemoveStringItem(string s) { return RemoveArrayItem(StringHelper.SplitString(s), "", true, false); } /// /// 移除數(shù)組中的重復(fù)項(xiàng) /// ///public static int[] RemoveRepeaterItem(int[] array) { if (array == null || array.Length < 2) return array; Array.Sort(array); int length = 1; for (int i = 1; i < array.Length; i++) { if (array[i] != array[i - 1]) length++; } int[] uniqueArray = new int[length]; uniqueArray[0] = array[0]; int j = 1; for (int i = 1; i < array.Length; i++) if (array[i] != array[i - 1]) uniqueArray[j++] = array[i]; return uniqueArray; } /// /// 移除數(shù)組中的重復(fù)項(xiàng) /// ///public static string[] RemoveRepeaterItem(string[] array) { if (array == null || array.Length < 2) return array; Array.Sort(array); int length = 1; for (int i = 1; i < array.Length; i++) { if (array[i] != array[i - 1]) length++; } string[] uniqueArray = new string[length]; uniqueArray[0] = array[0]; int j = 1; for (int i = 1; i < array.Length; i++) if (array[i] != array[i - 1]) uniqueArray[j++] = array[i]; return uniqueArray; } /// /// 去除字符串中的重復(fù)元素 /// ///public static string GetUniqueString(string s) { return GetUniqueString(s, ","); } /// /// 去除字符串中的重復(fù)元素 /// ///public static string GetUniqueString(string s, string splitStr) { return ObjectArrayToString(RemoveRepeaterItem(StringHelper.SplitString(s, splitStr)), splitStr); } #endregion /// /// 去除字符串首尾處的空格、回車、換行符、制表符 /// public static string TBBRTrim(string str) { if (!string.IsNullOrEmpty(str)) return str.Trim().Trim('\r').Trim('\n').Trim('\t'); return string.Empty; } ////// 去除字符串中的空格、回車、換行符、制表符 /// public static string ClearTBBR(string str) { if (!string.IsNullOrEmpty(str)) return _tbbrRegex.Replace(str, ""); return string.Empty; } ////// 刪除字符串中的空行 /// ///public static string DeleteNullOrSpaceRow(string s) { if (string.IsNullOrEmpty(s)) return ""; string[] tempArray = StringHelper.SplitString(s, "\r\n"); StringBuilder result = new StringBuilder(); foreach (string item in tempArray) { if (!string.IsNullOrWhiteSpace(item)) result.AppendFormat("{0}\r\n", item); } if (result.Length > 0) result.Remove(result.Length - 2, 2); return result.ToString(); } /// /// 獲得指定數(shù)量的html空格 /// ///public static string GetHtmlBS(int count) { if (count == 1) return " "; else if (count == 2) return " "; else if (count == 3) return " "; else { StringBuilder result = new StringBuilder(); for (int i = 0; i < count; i++) result.Append(" "); return result.ToString(); } } /// /// 獲得指定數(shù)量的htmlSpan元素 /// ///public static string GetHtmlSpan(int count) { if (count <= 0) return ""; if (count == 1) return ""; else if (count == 2) return ""; else if (count == 3) return ""; else { StringBuilder result = new StringBuilder(); for (int i = 0; i < count; i++) result.Append(""); return result.ToString(); } } /// ///獲得郵箱提供者 /// /// 郵箱 ///public static string GetEmailProvider(string email) { int index = email.LastIndexOf('@'); if (index > 0) return email.Substring(index + 1); return string.Empty; } /// /// 轉(zhuǎn)義正則表達(dá)式 /// public static string EscapeRegex(string s) { string[] oList = { "\\", ".", "+", "*", "?", "{", "}", "[", "^", "]", "$", "(", ")", "=", "!", "<", ">", "|", ":" }; string[] eList = { "\\\\", "\\.", "\\+", "\\*", "\\?", "\\{", "\\}", "\\[", "\\^", "\\]", "\\$", "\\(", "\\)", "\\=", "\\!", "\\<", "\\>", "\\|", "\\:" }; for (int i = 0; i < oList.Length; i++) s = s.Replace(oList[i], eList[i]); return s; } ////// 將ip地址轉(zhuǎn)換成long類型 /// /// ip ///public static long ConvertIPToLong(string ip) { string[] ips = ip.Split('.'); long number = 16777216L * long.Parse(ips[0]) + 65536L * long.Parse(ips[1]) + 256 * long.Parse(ips[2]) + long.Parse(ips[3]); return number; } /// /// 隱藏郵箱 /// public static string HideEmail(string email) { int index = email.LastIndexOf('@'); if (index == 1) return "*" + email.Substring(index); if (index == 2) return email[0] + "*" + email.Substring(index); StringBuilder sb = new StringBuilder(); sb.Append(email.Substring(0, 2)); int count = index - 2; while (count > 0) { sb.Append("*"); count--; } sb.Append(email.Substring(index)); return sb.ToString(); } ////// 隱藏手機(jī) /// public static string HideMobile(string mobile) { if (mobile != null && mobile.Length > 10) return mobile.Substring(0, 3) + "*****" + mobile.Substring(8); return string.Empty; } ////// 數(shù)據(jù)轉(zhuǎn)換為列表 /// /// 數(shù)組 ///public static List ArrayToList (T[] array) { List list = new List (array.Length); foreach (T item in array) list.Add(item); return list; } /// /// DataTable轉(zhuǎn)化為L(zhǎng)ist /// /// DataTable ///public static List > DataTableToList(DataTable dt) { int columnCount = dt.Columns.Count; List > list = new List >(dt.Rows.Count); foreach (DataRow dr in dt.Rows) { Dictionary item = new Dictionary (columnCount); for (int i = 0; i < columnCount; i++) { item.Add(dt.Columns[i].ColumnName, dr[i]); } list.Add(item); } return list; } }
以上就是如何使用字符操作普通幫助類,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。