/// <summary> /// 轉型為數值。 /// </summary> /// <param name="value">要轉型的值。</param> public static object CNum(object value) { // Null 傳回 0 if (IsNull(value)) { return(0); } // 針對文字型別做處理 if (value is string) { // 空字串傳回 0 if (StrFunc.StrIsEmpty(value)) { return(0); } // 若為數值先轉型為 Double 型別,防止有小數點無法轉型為整數 if (BaseFunc.IsNumeric(value)) { value = Convert.ToDouble(value); } } if (!BaseFunc.IsNumeric(value)) { throw new Exception(value + " 無法轉型為數值"); } return(value); }
/// <summary> /// 取得下一個流水號。 /// </summary> /// <param name="value">目前編號。</param> /// <param name="baseValues">編碼基底字串。</param> public static string GetNextID(string value, string baseValues) { string sValue; char[] oBaseValues; int iCount; int iIndex; oBaseValues = baseValues.ToCharArray(); iCount = oBaseValues.Length; sValue = StrFunc.StrTrim(value); for (int N1 = sValue.Length - 1; N1 >= 0; N1--) { iIndex = Array.IndexOf(oBaseValues, sValue[N1]); if (iIndex != -1 && iIndex < iCount - 1) { return(StrFunc.StrFormat("{0}{1}{2}", StrFunc.StrLeft(sValue, N1), oBaseValues[iIndex + 1], StrFunc.StrSubstring(sValue, N1 + 1))); } else { // 需要進位,將目前位數歸零 if (iIndex != -1 && iIndex < iCount) { sValue = StrFunc.StrFormat("{0}{1}{2}", StrFunc.StrLeft(sValue, N1), oBaseValues[0], StrFunc.StrSubstring(sValue, N1 + 1)); if (N1 == 0) { return(oBaseValues[0] + sValue); } } } } return(string.Empty); }
/// <summary> /// DES字串解密(隨機密碼)。 /// </summary> /// <param name="str">加密的字串。</param> /// <param name="result">傳出是否解密成功。</param> public static string DESDecrypt(string str, out bool result) { string sPassword = string.Empty; string sEncrypt = string.Empty; string sDecrypt; string sValue; result = false; if (StrFunc.StrIsEmpty(str)) { return(string.Empty); } if (str.Length < 10) { return(str); } //密碼 sPassword = StrFunc.StrLeft(str, 2) + StrFunc.StrSubstring(str, 10, 4) + StrFunc.StrRight(str, 2); //加密字串 sValue = StrFunc.StrLeftRightCut(str, 2); sEncrypt = StrFunc.StrSubstring(sValue, 0, 8) + StrFunc.StrSubstring(sValue, 12, sValue.Length - 12); try { sDecrypt = DESDecrypt(sEncrypt, sPassword); result = true; return(sDecrypt); } catch { return(str); } }
/// <summary> /// 字串解密。 /// </summary> /// <param name="str">加密的字串。</param> /// <param name="password">密碼。</param> public static string DESDecrypt(string str, string password) { DESCryptoServiceProvider oDESCrypto = null; int iLen = 0; int N1 = 0; int iByte = 0; System.IO.MemoryStream oMemoryStream = null; CryptoStream oCryptoStream = null; if (StrFunc.StrIsEmpty(str)) { return(string.Empty); } oDESCrypto = new DESCryptoServiceProvider(); //將字串放入 Byte 數組。 iLen = Convert.ToInt32(str.Length / 2) - 1; byte[] inputByteArray = new byte[iLen + 1]; for (N1 = 0; N1 <= iLen; N1++) { iByte = Convert.ToInt32(str.Substring(N1 * 2, 2), 16); inputByteArray[N1] = Convert.ToByte(iByte); } //建立加密對象的密碼和初始化向量 oDESCrypto.Key = ASCIIEncoding.ASCII.GetBytes(password); oDESCrypto.IV = ASCIIEncoding.ASCII.GetBytes(password); oMemoryStream = new System.IO.MemoryStream(); oCryptoStream = new CryptoStream(oMemoryStream, oDESCrypto.CreateDecryptor(), CryptoStreamMode.Write); oCryptoStream.Write(inputByteArray, 0, inputByteArray.Length); oCryptoStream.FlushFinalBlock(); return(Encoding.Default.GetString(oMemoryStream.ToArray())); }
/// <summary> /// 取得無 DbNull 的欄位值,組成以指定分隔符號分隔的字串 /// </summary> /// <param name="rows">資料列陣列</param> /// <param name="fieldName">欄位名稱</param> /// <param name="delimiter">分隔符號</param> /// <param name="checkEmpty">檢查空白</param> /// <returns></returns> public static string GetFieldValues(IEnumerable <DataRow> rows, string fieldName, string delimiter, bool checkEmpty = true) { var result = string.Empty; if (rows.Count() == 0 || !HasField(rows.First(), fieldName)) { return(result); } foreach (var row in rows) { if (row.RowState != DataRowState.Deleted) { var value = BaseFunc.CStr(GetFieldValueNotDbNull(row, fieldName)); if (checkEmpty && StrFunc.StrIsEmpty(value)) { continue; } result = result.StrMerge(value, delimiter); } } return(result); }
/// <summary> /// 由參考字串中隨機取得一個字元。 /// </summary> /// <param name="s">參考字串。</param> public static string RndWord(string s) { int iIndex = 0; iIndex = RndInt(1, s.Length); return(StrFunc.StrSubstring(s, iIndex, 1)); }
/// <summary> /// 取得無 DbNull 的欄位值,組成以逗號分隔的字串 /// </summary> /// <param name="rows">資料列陣列</param> /// <param name="fieldName">欄位名稱</param> /// <param name="checkEmpty">檢查空白</param> /// <param name="isSqlStr">是否用單引號包起來</param> /// <param name="distinct">是否過濾同樣資料</param> /// <returns></returns> public static string GetFieldValues(IEnumerable <DataRow> rows, string fieldName, bool checkEmpty = true, bool isSqlStr = true, bool distinct = false) { string result = string.Empty; if (rows.Count() != 0) { if (HasField(rows.First(), fieldName)) { foreach (DataRow row in rows) { if (row.RowState != DataRowState.Deleted) { string value = BaseFunc.CStr(GetFieldValueNotDbNull(row, fieldName)); if (checkEmpty && StrFunc.StrIsEmpty(value)) { continue; } else { value = isSqlStr ? StrFunc.SQLStr(value) : value; if (distinct && StrFunc.StrContains(result, value)) { continue; } result += (StrFunc.StrIsNotEmpty(result) ? "," : "") + value; } } } } } return(result); }
/// <summary> /// 建立欄位並加入資料表中。 /// </summary> /// <param name="fieldName">欄位名稱。</param> /// <param name="caption">欄位標題。</param> /// <param name="dataType">資料型別。</param> /// <param name="defaultValue">預設值。</param> /// <param name="dateTimeMode">設定資料行的 DateTimeMode。</param> public DataColumn AddColumn(string fieldName, string caption, Type dataType, object defaultValue, DataSetDateTime dateTimeMode = DataSetDateTime.Unspecified) { DataColumn oDataColumn; string sFieldName; // 欄位名稱全轉為大寫 sFieldName = StrFunc.StrUpper(fieldName); oDataColumn = new DataColumn(sFieldName, dataType); oDataColumn.DefaultValue = defaultValue; if (dataType == typeof(DateTime)) { oDataColumn.DateTimeMode = dateTimeMode; } if (!BaseFunc.IsDBNull(defaultValue)) { oDataColumn.AllowDBNull = false; } if (StrFunc.StrIsNotEmpty(caption)) { oDataColumn.Caption = caption; } this.DataTable.Columns.Add(oDataColumn); return(oDataColumn); }
/// <summary> /// 判斷二筆資料的欄位值是否相同。 /// </summary> /// <param name="value1">第一個欄位值。</param> /// <param name="value2">第二個欄位值。</param> /// <param name="ignoreCase">忽略大小寫。</param> public static bool FieldValueIsEquals(object[] value1, object[] value2, bool ignoreCase) { object oValue1; object oValue2; for (int N1 = 0; N1 < value1.Length; N1++) { oValue1 = value1[N1]; oValue2 = value2[N1]; if (ignoreCase && (oValue1 is string) && (oValue2 is string)) { if (!StrFunc.SameText(oValue1, oValue2)) { return(false); } } else { if (!FieldValueIsEquals(oValue1, oValue2)) { return(false); } } } return(true); }
/// <summary> /// 取得下一個流水號。 /// </summary> /// <param name="value">目前編號。</param> /// <param name="numberBase">流水號進位基底,2至36進位。</param> public static string GetNextID(string value, int numberBase) { string sValue; char[] oBaseValues; int iBaseValue; oBaseValues = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); sValue = StrFunc.StrTrim(value); for (int N1 = sValue.Length - 1; N1 >= 0; N1--) { iBaseValue = Array.IndexOf(oBaseValues, sValue[N1]); if (iBaseValue != -1 && iBaseValue < numberBase - 1) { return(StrFunc.StrFormat("{0}{1}{2}", StrFunc.StrLeft(sValue, N1), oBaseValues[iBaseValue + 1], StrFunc.StrSubstring(sValue, N1 + 1))); } else { // 需要進位,將目前位數歸零 if (iBaseValue != -1 && iBaseValue < numberBase) { sValue = StrFunc.StrFormat("{0}{1}{2}", StrFunc.StrLeft(sValue, N1), "0", StrFunc.StrSubstring(sValue, N1 + 1)); } } } return(string.Empty); }
/// <summary> /// 使用 SaltHash 演算法進行資料加密。 /// </summary> /// <param name="s">原字串。</param> /// <param name="key">加密鍵值。</param> public static string SaltHashEncrypt(string s, string key = "") { string sSalted = key; byte[] oOriginal = null; byte[] oSaltValue = null; byte[] oToSalt = null; MD5 oMD5 = null; byte[] oSaltPWD = null; byte[] oPWD = null; if (StrFunc.StrIsEmpty(sSalted)) { sSalted = "scskxmd"; } oOriginal = Encoding.Default.GetBytes(s); oSaltValue = Encoding.Default.GetBytes(sSalted); oToSalt = new byte[oOriginal.Length + oSaltValue.Length]; oOriginal.CopyTo(oToSalt, 0); oSaltValue.CopyTo(oToSalt, oOriginal.Length); oMD5 = MD5.Create(); oSaltPWD = oMD5.ComputeHash(oToSalt); oPWD = new byte[oSaltPWD.Length + oSaltValue.Length]; oSaltPWD.CopyTo(oPWD, 0); oSaltValue.CopyTo(oPWD, oSaltPWD.Length); return(Convert.ToBase64String(oPWD)); }
/// <summary> /// 由來源資料列異動目前資料列,目的資料列存在相同欄位,且不在排除欄位中時才會進行異動。 /// </summary> /// <param name="sourceRow">來源資料列。</param> /// <param name="destRow">目的資料列。</param> /// <param name="excludeFields">排除欄位</param> public static void UpdateRow(DataRow sourceRow, DataRow destRow, params string[] excludeFields) { DataTable oSoruceTable; DataTable oDestTable; if (BaseFunc.IsEmpty(sourceRow)) { return; } if (BaseFunc.IsEmpty(destRow)) { return; } oSoruceTable = sourceRow.Table; oDestTable = destRow.Table; foreach (DataColumn oColumn in oSoruceTable.Columns) { //確認該欄位是否存在於排除欄位中 var matchExcludeFields = from excludeField in excludeFields where StrFunc.SameText(excludeField, oColumn.ColumnName) select excludeField; if (matchExcludeFields.ToList().Count == 0 && oDestTable.Columns.Contains(oColumn.ColumnName) && !BaseFunc.IsNullOrDBNull(sourceRow[oColumn.ColumnName])) { destRow[oColumn.ColumnName] = sourceRow[oColumn.ColumnName]; } } }
/// <summary> /// 字串轉型為布林值。 /// </summary> /// <param name="value">要轉型的值。</param> public static bool CBool(string value) { if (StrFunc.SameText(value, "Y")) { return(true); } else if (StrFunc.SameText(value, "T")) { return(true); } else if (StrFunc.SameText(value, "true")) { return(true); } else if (StrFunc.SameText(value, "1")) { return(true); } else if (StrFunc.SameText(value, "是")) { return(true); } else if (StrFunc.SameText(value, "真")) { return(true); } else { return(false); } }
/// <summary> /// 取得不重覆資料。 /// </summary> /// <param name="dataTable">資料表。</param> /// <param name="fieldNames">判斷重覆的欄位名稱集合字串,以逗點分隔欄位名稱。</param> public static DataTable Distinct(DataTable dataTable, string fieldNames) { string[] oFieldNames; oFieldNames = StrFunc.StrSplit(fieldNames, ","); return(dataTable.DefaultView.ToTable(true, oFieldNames)); }
/// <summary> /// 轉換為 DateTime 型別。 /// </summary> /// <param name="date">日期字串。</param> /// <param name="time">時間字串。</param> public static DateTime ToDateTime(string date, string time) { string sValue; string sTime; sTime = time; // 若時間是四碼或六碼數值,則加上時間冒號 if (BaseFunc.IsNumeric(sTime)) { if (StrFunc.StrLen(sTime) == 4) { sTime = StrFunc.StrFormat("{0}:{1}", StrFunc.StrLeft(sTime, 2), StrFunc.StrSubstring(sTime, 2, 2)); } else if (StrFunc.StrLen(sTime) == 6) { sTime = StrFunc.StrFormat("{0}:{1}:{2}", StrFunc.StrLeft(sTime, 2), StrFunc.StrSubstring(sTime, 2, 2), StrFunc.StrSubstring(sTime, 4, 2)); } } sValue = StrFunc.StrFormat("{0} {1}", date, sTime); if (IsDate(sValue)) { return(BaseFunc.CDateTime(sValue)); } else { return(DateTime.MinValue); } }
/// <summary> /// 依分隔符號合併二個字串。 /// </summary> /// <param name="s1">第一個字串。</param> /// <param name="s2">第二個字串。</param> /// <param name="delimiter">分隔符號。</param> public static string StrMerge(string s1, string s2, string delimiter) { if (StrFunc.StrIsNotEmpty(s1)) { s1 = s1 + delimiter; } return(s1 + s2); }
/// <summary> /// 是否為空值,Null 或 DBNull 皆視為空值。 /// </summary> /// <param name="value">要判斷的值。</param> public static bool IsEmpty(object value) { if (value is string) { return(StrFunc.StrIsEmpty(BaseFunc.CStr(value))); } else { return(IsNull(value)); } }
/// <summary> /// 將字串前後加上單引號 /// </summary> /// <param name="source"></param> /// <returns></returns> public static string SQLStr(this string source) { if (StrFunc.StrIsNotEmpty(source)) { return(StrFunc.SQLStr(source)); } else { return("''"); } }
/// <summary> /// 判斷陣列所有成員都不是空字串。 /// </summary> /// <param name="args">字串陣列。</param> public static bool StrIsNotEmpty(params string[] args) { foreach (string s in args) { // 任何一個成員為空字串則傳回 false if (StrFunc.StrIsEmpty(s)) { return(false); } } return(true); }
/// <summary> /// 轉型成時間間格值 /// </summary> /// <param name="time"></param> /// <returns></returns> public static TimeSpan CTimeSpan(string time) { var result = new TimeSpan(); if (StrFunc.StrIsNotEmpty(time)) { int hours = BaseFunc.CInt(StrFunc.StrLeft(time, 2)); int minutes = BaseFunc.CInt(StrFunc.StrRight(time, 2)); result = new TimeSpan(hours, minutes, 0); } return(result); }
/// <summary> /// 西曆轉農曆 /// </summary> /// <param name="value"></param> /// <returns></returns> public static string ToLunarDate(DateTime value) { var oLCalendar = new TaiwanLunisolarCalendar(); var y = oLCalendar.GetYear(value); var m = oLCalendar.GetMonth(value); var d = oLCalendar.GetDayOfMonth(value); var l = y.ToString() + StrFunc.StrPadLeft(m.ToString(), 2, '0') + StrFunc.StrPadLeft(d.ToString(), 2, '0'); return(l); }
/// <summary> /// 設定資料表的主索引鍵。 /// </summary> /// <param name="dataTable">資料表。</param> /// <param name="fieldNames">主索引鍵的欄位集合字串(以逗點分隔多個欄位名稱)。</param> public static void DataTableSetPrimaryKey(DataTable dataTable, string fieldNames) { string[] oFieldNames; DataColumn[] oDataColumns; int iIndex = 0; oFieldNames = StrFunc.StrSplit(fieldNames); oDataColumns = new DataColumn[oFieldNames.Length]; foreach (string sFieldName in oFieldNames) { oDataColumns[iIndex] = dataTable.Columns[sFieldName]; iIndex++; } dataTable.PrimaryKey = oDataColumns; }
/// <summary> /// 加入集合字串為成員。 /// </summary> /// <param name="s">字串。</param> /// <param name="delimiter">分隔符號。</param> public void Add(string s, string delimiter) { if (StrFunc.StrIsEmpty(s)) { return; } var values = StrFunc.StrSplit(s, delimiter); foreach (var value in values) { if (!this.Contains(value)) { this.Add(value); } } }
/// <summary> /// 判斷資料集是否無資料,為 Null 或資料表數為零,皆視為無資料。 /// </summary> /// <param name="dataSet">要判斷的資料表。</param> public static bool IsEmpty(DataSet dataSet) { //資料集為 Null 或無資料表,皆視為無資料 if (BaseFunc.IsNull(dataSet) || (dataSet.Tables.Count == 0)) { return(true); } // 主檔資料表無資料時,也視為無資料 if (StrFunc.StrIsNotEmpty(dataSet.DataSetName) && dataSet.Tables.Contains(dataSet.DataSetName)) { if (IsEmpty(dataSet.Tables[dataSet.DataSetName])) { return(true); } } return(false); }
/// <summary> /// DES字串加密(隨機密碼)。 /// </summary> /// <param name="str">未加密的文字。</param> public static string DESEncrypt(string str) { string s = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string sPassword = string.Empty; string sEncrypt = string.Empty; //空字串無法加密,直接回傳空字串。 if (StrFunc.StrIsEmpty(str)) { return(string.Empty); } sPassword = BaseFunc.RndString(s, 8); sEncrypt = DESEncrypt(str, sPassword); //將密碼儲存於加密字串中 sEncrypt = StrFunc.StrLeft(sPassword, 2) + sEncrypt.Insert(8, StrFunc.StrSubstring(sPassword, 2, 4)) + StrFunc.StrRight(sPassword, 2); return(sEncrypt); }
/// <summary> /// 字串轉16進位字串。 /// </summary> /// <param name="value">要轉的值。</param> public static string CHex(string value) { char[] oChars; string sReturnValue; if (StrFunc.StrIsEmpty(value)) { return(string.Empty); } sReturnValue = string.Empty; oChars = value.ToCharArray(); foreach (char letter in oChars) { sReturnValue += Convert.ToString(letter, 16); } return(sReturnValue); }
/// <summary> /// 轉型為 Guid 值。 /// </summary> /// <param name="value">要轉型的值。</param> public static Guid CGuid(string value) { if (IsEmpty(value) || StrFunc.SameText(value, "*")) { return(Guid.Empty); } else { var result = Guid.Empty; if (Guid.TryParse(value, out result)) { return(result); } else { return(result); } } }
/// <summary> /// 取得字串右邊指定長度的子字串。 /// </summary> /// <param name="s">字串。</param> /// <param name="length">長度。</param> public static string StrRight(string s, int length) { int iStartIndex; // 若字串為空或擷取長度為 0,則傳回空字串 if (StrIsEmpty(s) || (length <= 0)) { return(string.Empty); } // 若擷取長度大於字串長度,則傳回原字串 if (length > s.Length) { return(s); } //計算擷取字串的起始位置 iStartIndex = s.Length - length; //在字串中擷取指定起始位置後子字串 return(StrFunc.StrSubstring(s, iStartIndex)); }
/// <summary> /// 拆解字串的資料表名稱及欄位名稱。 /// </summary> /// <param name="s">字串。</param> /// <param name="tableName">資料表名稱。</param> /// <param name="fieldName">欄位名稱。</param> public static void StrSplitFieldName(string s, out string tableName, out string fieldName) { string[] oValues; oValues = StrFunc.StrSplit(s, "."); if (oValues.Length == 1) { tableName = string.Empty; fieldName = oValues[0]; } else if (oValues.Length == 2) { tableName = oValues[0]; fieldName = oValues[1]; } else { throw new GException("錯誤的欄位名欄位 {0}", s); } }
/// <summary> /// 判斷傳入字串是否等於陣列中任一成員。 /// </summary> /// <param name="s">字串。</param> /// <param name="args">以逗點分隔的字串集合。</param> public static bool SameTextOr(string s, string args) { return(SameTextOr(s, StrFunc.StrSplit(args))); }