字元如何操作普通幫助類別?字元操作普通幫助類別的方法(程式碼範例)
本篇文章给大家带来的内容是介绍字符如何操作普通帮助类?字符操作普通帮助类的方法(代码示例)。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。
/// <summary> /// 普通帮助类 /// </summary> public class CommonHelper { //星期数组 private static string[] _weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; //空格、回车、换行符、制表符正则表达式 private static Regex _tbbrRegex = new Regex(@"\s*|\t|\r|\n", RegexOptions.IgnoreCase); #region 时间操作 /// <summary> /// 获得当前时间的""yyyy-MM-dd HH:mm:ss:fffffff""格式字符串 /// </summary> public static string GetDateTimeMS() { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffffff"); } /// <summary> /// 获得当前时间的""yyyy年MM月dd日 HH:mm:ss""格式字符串 /// </summary> public static string GetDateTimeU() { return string.Format("{0:U}", DateTime.Now); } /// <summary> /// 获得当前时间的""yyyy-MM-dd HH:mm:ss""格式字符串 /// </summary> public static string GetDateTime() { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } /// <summary> /// 获得当前日期 /// </summary> public static string GetDate() { return DateTime.Now.ToString("yyyy-MM-dd"); } /// <summary> /// 获得中文当前日期 /// </summary> public static string GetChineseDate() { return DateTime.Now.ToString("yyyy月MM日dd"); } /// <summary> /// 获得当前时间(不含日期部分) /// </summary> public static string GetTime() { return DateTime.Now.ToString("HH:mm:ss"); } /// <summary> /// 获得当前小时 /// </summary> public static string GetHour() { return DateTime.Now.Hour.ToString("00"); } /// <summary> /// 获得当前天 /// </summary> public static string GetDay() { return DateTime.Now.Day.ToString("00"); } /// <summary> /// 获得当前月 /// </summary> public static string GetMonth() { return DateTime.Now.Month.ToString("00"); } /// <summary> /// 获得当前年 /// </summary> public static string GetYear() { return DateTime.Now.Year.ToString(); } /// <summary> /// 获得当前星期(数字) /// </summary> public static string GetDayOfWeek() { return ((int)DateTime.Now.DayOfWeek).ToString(); } /// <summary> /// 获得当前星期(汉字) /// </summary> public static string GetWeek() { return _weekdays[(int)DateTime.Now.DayOfWeek]; } #endregion #region 数组操作 /// <summary> /// 获得字符串在字符串数组中的位置 /// </summary> 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; } /// <summary> /// 获得字符串在字符串数组中的位置 /// </summary> public static int GetIndexInArray(string s, string[] array) { return GetIndexInArray(s, array, false); } /// <summary> /// 判断字符串是否在字符串数组中 /// </summary> public static bool IsInArray(string s, string[] array, bool ignoreCase) { return GetIndexInArray(s, array, ignoreCase) > -1; } /// <summary> /// 判断字符串是否在字符串数组中 /// </summary> public static bool IsInArray(string s, string[] array) { return IsInArray(s, array, false); } /// <summary> /// 判断字符串是否在字符串中 /// </summary> public static bool IsInArray(string s, string array, string splitStr, bool ignoreCase) { return IsInArray(s, StringHelper.SplitString(array, splitStr), ignoreCase); } /// <summary> /// 判断字符串是否在字符串中 /// </summary> public static bool IsInArray(string s, string array, string splitStr) { return IsInArray(s, StringHelper.SplitString(array, splitStr), false); } /// <summary> /// 判断字符串是否在字符串中 /// </summary> public static bool IsInArray(string s, string array) { return IsInArray(s, StringHelper.SplitString(array, ","), false); } /// <summary> /// 将对象数组拼接成字符串 /// </summary> 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(); } /// <summary> /// 将对象数组拼接成字符串 /// </summary> public static string ObjectArrayToString(object[] array) { return ObjectArrayToString(array, ","); } /// <summary> /// 将字符串数组拼接成字符串 /// </summary> public static string StringArrayToString(string[] array, string splitStr) { return ObjectArrayToString(array, splitStr); } /// <summary> /// 将字符串数组拼接成字符串 /// </summary> public static string StringArrayToString(string[] array) { return StringArrayToString(array, ","); } /// <summary> /// 将整数数组拼接成字符串 /// </summary> 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(); } /// <summary> /// 将整数数组拼接成字符串 /// </summary> public static string IntArrayToString(int[] array) { return IntArrayToString(array, ","); } /// <summary> /// 移除数组中的指定项 /// </summary> /// <param name="array">源数组</param> /// <param name="removeItem">要移除的项</param> /// <param name="removeBackspace">是否移除空格</param> /// <param name="ignoreCase">是否忽略大小写</param> /// <returns></returns> 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; } /// <summary> /// 移除数组中的指定项 /// </summary> /// <param name="array">源数组</param> /// <returns></returns> public static string[] RemoveArrayItem(string[] array) { return RemoveArrayItem(array, "", true, false); } /// <summary> /// 移除字符串中的指定项 /// </summary> /// <param name="s">源字符串</param> /// <param name="splitStr">分割字符串</param> /// <returns></returns> public static string[] RemoveStringItem(string s, string splitStr) { return RemoveArrayItem(StringHelper.SplitString(s, splitStr), "", true, false); } /// <summary> /// 移除字符串中的指定项 /// </summary> /// <param name="s">源字符串</param> /// <returns></returns> public static string[] RemoveStringItem(string s) { return RemoveArrayItem(StringHelper.SplitString(s), "", true, false); } /// <summary> /// 移除数组中的重复项 /// </summary> /// <returns></returns> 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; } /// <summary> /// 移除数组中的重复项 /// </summary> /// <returns></returns> 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; } /// <summary> /// 去除字符串中的重复元素 /// </summary> /// <returns></returns> public static string GetUniqueString(string s) { return GetUniqueString(s, ","); } /// <summary> /// 去除字符串中的重复元素 /// </summary> /// <returns></returns> public static string GetUniqueString(string s, string splitStr) { return ObjectArrayToString(RemoveRepeaterItem(StringHelper.SplitString(s, splitStr)), splitStr); } #endregion /// <summary> /// 去除字符串首尾处的空格、回车、换行符、制表符 /// </summary> public static string TBBRTrim(string str) { if (!string.IsNullOrEmpty(str)) return str.Trim().Trim('\r').Trim('\n').Trim('\t'); return string.Empty; } /// <summary> /// 去除字符串中的空格、回车、换行符、制表符 /// </summary> public static string ClearTBBR(string str) { if (!string.IsNullOrEmpty(str)) return _tbbrRegex.Replace(str, ""); return string.Empty; } /// <summary> /// 删除字符串中的空行 /// </summary> /// <returns></returns> 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(); } /// <summary> /// 获得指定数量的html空格 /// </summary> /// <returns></returns> 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(); } } /// <summary> /// 获得指定数量的htmlSpan元素 /// </summary> /// <returns></returns> public static string GetHtmlSpan(int count) { if (count <= 0) return ""; if (count == 1) return "<span></span>"; else if (count == 2) return "<span></span><span></span>"; else if (count == 3) return "<span></span><span></span><span></span>"; else { StringBuilder result = new StringBuilder(); for (int i = 0; i < count; i++) result.Append("<span></span>"); return result.ToString(); } } /// <summary> ///获得邮箱提供者 /// </summary> /// <param name="email">邮箱</param> /// <returns></returns> public static string GetEmailProvider(string email) { int index = email.LastIndexOf('@'); if (index > 0) return email.Substring(index + 1); return string.Empty; } /// <summary> /// 转义正则表达式 /// </summary> 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; } /// <summary> /// 将ip地址转换成long类型 /// </summary> /// <param name="ip">ip</param> /// <returns></returns> 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; } /// <summary> /// 隐藏邮箱 /// </summary> 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(); } /// <summary> /// 隐藏手机 /// </summary> public static string HideMobile(string mobile) { if (mobile != null && mobile.Length > 10) return mobile.Substring(0, 3) + "*****" + mobile.Substring(8); return string.Empty; } /// <summary> /// 数据转换为列表 /// </summary> /// <param name="array">数组</param> /// <returns></returns> public static List<T> ArrayToList<T>(T[] array) { List<T> list = new List<T>(array.Length); foreach (T item in array) list.Add(item); return list; } /// <summary> /// DataTable转化为List /// </summary> /// <param name="dt">DataTable</param> /// <returns></returns> public static List<Dictionary<string, object>> DataTableToList(DataTable dt) { int columnCount = dt.Columns.Count; List<Dictionary<string, object>> list = new List<Dictionary<string, object>>(dt.Rows.Count); foreach (DataRow dr in dt.Rows) { Dictionary<string, object> item = new Dictionary<string, object>(columnCount); for (int i = 0; i < columnCount; i++) { item.Add(dt.Columns[i].ColumnName, dr[i]); } list.Add(item); } return list; } }
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关视频教程可以访问:ASP.NET视频教程!
以上是字元如何操作普通幫助類別?字元操作普通幫助類別的方法(程式碼範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

使用Java的Character.isDigit()函數判斷字元是否為數字字元在電腦內部以ASCII碼的形式表示,每個字元都有一個對應的ASCII碼。其中,數字字元0到9分別對應的ASCII碼值為48到57。要判斷一個字元是否為數字,可以使用Java中的Character類別提供的isDigit()方法來判斷。 isDigit()方法是Character類別的

如何使用自動更正在 Word 中鍵入箭頭在 Word 中鍵入箭頭的最快方法之一是使用預先定義的自動修正捷徑。如果您鍵入特定的字元序列,Word 會自動將這些字元轉換為箭頭符號。您可以使用此方法繪製多種不同的箭頭樣式。若要使用自動更正在 Word 中鍵入箭頭:將遊標移到文件中要顯示箭頭的位置。鍵入以下字元組合之一:如果您不希望將您鍵入的內容更正為箭頭符號,請按鍵盤上的退格鍵會將

您的實體或數位鍵盤在表面上提供有限數量的字元選項。但是,有幾種方法可以在iPhone、iPad和Mac上存取重音字母、特殊字元等。標準iOS鍵盤可讓您快速存取大寫和小寫字母、標準數字、標點符號和字元。當然,還有很多其他角色。您可以從帶有變音符號的字母到倒置的問號中進行選擇。您可能無意中發現了隱藏的特殊字元。如果沒有,以下是在iPhone、iPad和Mac上存取它們的方法。如何在iPhone和iPad上存取擴充字元在iPhone或iPad上取得擴充字元非常簡單。在「訊息」、「

在matplotlib中正確地顯示中文字符,是許多中文使用者常常遇到的問題。預設情況下,matplotlib使用的是英文字體,無法正確顯示中文字元。為了解決這個問題,我們需要設定正確的中文字體,並將其應用到matplotlib中。以下是一些具體的程式碼範例,幫助你正確地在matplotlib中顯示中文字元。首先,我們需要導入需要的函式庫:importmatplot

上標是一個字符或多個字符,可以是字母或數字,您需要將其設置為略高於正常文本行。例如,如果您需要寫1st,則字母st需要略高於字元1。同樣,下標是一組字符或單個字符,需要設置為略低於正常文本級別。例如,當你寫化學式時,你需要把數字放在正常字元行的下方。以下螢幕截圖顯示了上標和下標格式的一些範例。儘管這似乎是一項艱鉅的任務,但實際上將上標和下標格式應用於您的文字非常簡單。在本文中,我們將透過一些簡單的步驟說明如何輕鬆地使用上標或下標格式設定文字。希望你喜歡閱讀這篇文章。如何在 Excel 中套用上標

如何使用Golang判斷一個字元是否為字母在Golang中,判斷一個字元是否為字母可以透過使用Unicode包中的IsLetter函數來實現。 IsLetter函數會檢查給定的字元是否為字母。接下來,我們將詳細介紹如何使用Golang編寫程式碼來判斷一個字元是否為字母。首先,你需要建立一個新的Go文件,用於編寫程式碼。你可以將檔案命名為"main.go"。程式碼

Java中回車鍵的字元表示是`。在Java中,`表示換行符,當遇到這個字元時,文字輸出會換行。以下是一個簡單的程式碼範例,示範如何使用``來表示回車鍵:publicclassMain{publicstaticvoidmain(String[]args){System.out.println("這是第一行這

在平板電腦模式下啟用觸控鍵盤如果您有觸控螢幕筆記型電腦,則可以使用觸控鍵盤在Windows11上鍵入多個特殊字元。這可能是添加特殊字元最簡單的方法。在Windows11上啟用特殊字元的觸控螢幕:開啟開始功能表並選擇設定。當設定開啟時,導航到時間和語言>打字>觸摸鍵盤。在「鍵入」功能表中,勾選「沒有鍵盤時顯示觸控鍵盤」選項。啟用無平板電腦模式的觸控鍵盤存取觸控鍵盤的另一種方法是使其全時顯示在工作列上。要使觸控鍵盤可訪問,您需要告訴Windows11顯示它。使用以下步驟:在開始功能表中,選
