乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      CheckData字符類方法(字符串操作)

       悟靜 2013-01-09

      /// <summary>
              /// 類擬substring一樣eg:GetCountString("abcde",1,3) 返回bcd
              /// </summary>
              /// <param name="lzpstr"></param>
              /// <param name="StartIndex"></param>
              /// <param name="length"></param>
              /// <returns></returns>
              public string GetCountString(string lzpstr, int StartIndex, int length)
              {
                  string s = "";
                  byte[] bytes = Encoding.Default.GetBytes(lzpstr);
                  if (StartIndex > bytes.Length)
                  {
                      return "";
                  }
                  if (length > (bytes.Length - StartIndex))
                  {
                      length = bytes.Length - StartIndex;
                  }
                  s = Encoding.Default.GetString(bytes, StartIndex, length);
                  if (Encoding.Default.GetBytes(s).Length < length)
                  {
                      s = Encoding.Default.GetString(bytes, StartIndex, length + 1);
                  }
                  return s;
              }
              /// <summary>
              /// 返回分隔符最后一個字串 eg:GetShortFileName("ab,cd,efg",',') 返回efg
              /// </summary>
              /// <param name="FileName"></param>
              /// <param name="spliter"></param>
              /// <returns></returns>
              public string GetShortFileName(string FileName, char spliter)
              {
                  string[] textArray = FileName.Split(new char[] { spliter });
                  return textArray[textArray.Length - 1].ToString();
              }
              /// <summary>
              /// 返回分隔符最后一個字串 跟 GetShortFileName類擬 默認以 . 為分隔符 eg:GetTailName("abc.defgh.ijh") 返回 ijh
              /// </summary>
              /// <param name="FileName"></param>
              /// <returns></returns>
              public string GetTailName(string FileName)
              {
                  string[] textArray = FileName.Split(new char[] { '.' });
                  return textArray[textArray.Length - 1].ToString();
              }

              /// <summary>
              /// 返回當前時間的秒eg:20070525020038 會隨時間而變化
              /// </summary>
              /// <returns></returns>
              public string GetUniqueFileName()
              {
                  return DateTime.Now.ToString("yyyyMMddhhmmss");
              }

       /// <summary>
              /// 默認:字符補在右邊
              /// </summary>
              /// <param name="str"></param>
              /// <param name="width"></param>
              /// <param name="compart"></param>
              /// <returns></returns>
              public static string FormatStr(string str, int width, string compart)
              {
                  return FormatStr(str, width, compart, false);
              }
              /// <summary>
              /// 格式化,返回定長字串
              /// </summary>
              /// <param name="str"></param>
              /// <param name="width">字串寬度</param>
              /// <param name="compart">不夠?qū)挾葧r補上的字符</param>
              /// <param name="isLeft">左對齊?default:true 補在右邊    </param>
              /// <returns></returns>
              public static string FormatStr(string str, int width, string compart, bool isLeft)
              {
                  int byteCount = Encoding.Default.GetByteCount(str);
                  string text = "";
                  for (int i = 0; i < (width - byteCount); i++)
                  {
                      text = text + compart;
                  }
                  if (isLeft)
                  {
                      return (text + str);
                  }
                  return (str + text);
              }
              /// <summary>
              /// 根據(jù)文件擴展名取得文件類型
              /// </summary>
              /// <param name="extend"></param>
              /// <returns></returns>
              private static string getFileType(string extend)
              {
                  switch (extend.ToLower())
                  {
                      case ".jpg":
                      case ".jpeg":
                          return "image/pjpeg";

                      case ".gif":
                          return "image/gif";

                      case ".png":
                          return "image/x-png";

                      case ".bmp":
                          return "image/bmp";

                      case ".txt":
                          return "text/plain";

                      case ".htm":
                      case ".html":
                          return "text/html";
                  }
                  return "";
              }
              /// <summary>
              /// 字符串編碼
              /// </summary>
              /// <param name="inputData"></param>
              /// <returns></returns>
              public static string HtmlEncode(string inputData)
              {
                  return HttpUtility.HtmlEncode(inputData);
              }
              /// <summary>
              /// 檢查郵件地址
              /// </summary>
              /// <param name="inputData">輸入字符串</param>
              /// <returns></returns>
              public static bool IsEmail(string inputData)
              {
                  return RegEmail.Match(inputData).Success;
              }
              /// <summary>
              /// 是否為空
              /// </summary>
              /// <param name="key">目標物件    </param>
              /// <returns></returns>
              public static bool IsEmpty(object key)
              {
                  return !ToType(key);
              }

              /// <summary>
              /// 檢測是否有中文字符
              /// </summary>
              /// <param name="inputData"></param>
              /// <returns></returns>
              public static bool IsHasCHZN(string inputData)
              {
                  return RegCHZN.Match(inputData).Success;
              }
              /// <summary>
              /// 是否為圖片類型        默認值: ".jpg",".gif",".bmp",".png"
              /// </summary>
              /// <param name="imgType">File.ContentType</param>
              /// <returns></returns>
              public static bool IsImage(string imgType)
              {
                  return IsImage(imgType, new string[] { ".jpg", ".gif", ".bmp", ".png" });
              }
              /// <summary>
              /// 是否為圖片類型
              /// </summary>
              /// <param name="imgType">File.ContentType</param>
              /// <param name="allowType">允許的文件擴展名 eg: ".jpg",".gif"</param>
              /// <returns></returns>
              public static bool IsImage(string imgType, params string[] allowType)
              {
                  imgType = imgType.ToLower();
                  for (int i = 0; i < allowType.Length; i++)
                  {
                      if (imgType == getFileType(allowType[i]))
                      {
                          return true;
                      }
                  }
                  return false;
              }
              /// <summary>
              /// 是否數(shù)字字符串 [0-9]
              /// </summary>
              /// <param name="inputData">輸入字符串</param>
              /// <returns></returns>
              public static bool IsNumber(string inputData)
              {
                  return RegNumber.Match(inputData).Success;
              }
              /// <summary>
              /// 是否數(shù)字字符串 可帶正負號 [0-9]
              /// </summary>
              /// <param name="inputData">輸入字符串</param>
              /// <returns></returns>
              public static bool IsNumberSign(string inputData)
              {
                  return RegNumberSign.Match(inputData).Success;
              }
      /// <summary>
              /// 檢查字符串最大長度,返回指定長度的串
              /// </summary>
              /// <param name="sqlInput"></param>
              /// <param name="maxLength"></param>
              /// <returns></returns>
              public static string SqlText(string sqlInput, int maxLength)
              {
                  if ((sqlInput != null) && (sqlInput != string.Empty))
                  {
                      sqlInput = sqlInput.Trim();
                      if (sqlInput.Length > maxLength)
                      {
                          sqlInput = sqlInput.Substring(0, maxLength);
                      }
                  }
                  return sqlInput;
              }
              /// <summary>
              /// 檢查從頁面輸入的字符串,防止SQL注入攻擊
              /// </summary>
              /// <param name="Sour"></param>
              /// <returns></returns>
              public static string CheckString(string Sour)
              {
                  return Sour.Replace("'", "''").Replace("--", "- - ");
              }
              /// <summary>
              /// 比較兩個 double 值 的大小返回 1|0|-1
              /// </summary>
              /// <param name="a"></param>
              /// <param name="b"></param>
              /// <returns></returns>
              public static int CompareDouble(double a, double b)
              {
                  if (Math.Abs((double) (a - b)) <= 1E-06)
                  {
                      return 0;
                  }
                  if (a > b)
                  {
                      return 1;
                  }
                  return -1;
              }
              /// <summary>
              /// if true then return 1 else return 0
              /// </summary>
              /// <param name="isTrue"></param>
              /// <returns></returns>
              public static string ConvertBoolToString(bool isTrue)
              {
                  if (isTrue)
                  {
                      return "1";
                  }
                  return "0";
              }
              /// <summary>
              /// 將空值轉(zhuǎn)換為"0"
              /// </summary>
              /// <param name="key"></param>
              /// <returns></returns>
              public static string ConvertEmptyTo0(object key)
              {
                  if (object.Equals(key, null))
                  {
                      return "0";
                  }
                  if (key.ToString() == "")
                  {
                      return "0";
                  }
                  return key.ToString();
              }
              /// <summary>
              /// if Value is null or "" ,return "0"
              /// </summary>
              /// <param name="key"></param>
              /// <returns></returns>
              public static string ConvertEmptyTo0(string key)
              {
                  if ((key != null) && (key != ""))
                  {
                      return key;
                  }
                  return "0";
              }
              /// <summary>
              /// if 1 then return true else return false
              /// </summary>
              /// <param name="key"></param>
              /// <returns></returns>
              public static bool ConvertStringToBool(string key)
              {
                  return (key == "1");
              }
              /// <summary>
              /// 返回有兩位小數(shù)的字串,eg: 1,234,567.89        為0則返回空
              /// </summary>
              /// <param name="num"></param>
              /// <returns></returns>
              public static string FormatDouble(double num)
              {
                  if (num == 0)
                  {
                      return "";
                  }
                  return string.Format("{0:N2}", num);
              }
              /// <summary>
              /// 返回有兩位小數(shù)的字串 eg: 1,234,567.89        為0則返回 0.00,且無千位分隔符
              /// </summary>
              /// <param name="num"></param>
              /// <param name="isZeroToEmpty">是否將"0"轉(zhuǎn)成"",Default:true</param>
              /// <returns></returns>
              public static string FormatDouble(object num, bool isZeroToEmpty)
              {
                  if (isZeroToEmpty)
                  {
                      return FormatDouble(num);
                  }
                  try
                  {
                      return string.Format("{0:F2}", ToDouble(num));
                  }
                  catch
                  {
                      return ToString(num);
                  }
              }
              /// <summary>
              /// 返回有兩位小數(shù)的字串 eg: 1,234,567.89    為0則返回空
              /// </summary>
              /// <param name="num"></param>
              /// <returns></returns>
              public static string FormatDouble(object num)
              {
                  try
                  {
                      return FormatDouble(ToDouble(num));
                  }
                  catch
                  {
                      return ToString(num);
                  }
              } 
      /// <summary>
              ///
              /// </summary>
              /// <param name="Sour"></param>
              /// <returns></returns>
              private static string CheckDate2(string Sour)
              {
                  string text = "";
                  if (Sour == "")
                  {
                      return "日期不能為空";
                  }
                  if (Sour.CompareTo(DateTime.Now.ToString("yyyy-MM-dd")) < 0)
                  {
                      return "日期不能小于當前日期";
                  }
                  Match match = new Regex("^([0-9]{4})-([0-9]{2})-([0-9]{2})$").Match(Sour);
                  if (match.Success)
                  {
                      int year = Convert.ToInt32(match.Groups[1].Value);
                      int month = Convert.ToInt32(match.Groups[2].Value);
                      int day = Convert.ToInt16(match.Groups[3].Value);
                      try
                      {
                          new DateTime(year, month, day);
                      }
                      catch
                      {
                          text = "日期格式不正確,應為【2002-09-06】";
                      }
                      return text;
                  }
                  return "日期格式不正確,應為【2002-09-06】";
              }
              /// <summary>
              /// 是否為數(shù)字,一組數(shù)字和對應的一組錯誤信息
              /// </summary>
              /// <param name="values">Values</param>
              /// <param name="texts">錯誤信息</param>
              /// <returns></returns>
              public static string CheckDouble(string[] values, string[] texts)
              {
                  string text = "";
                  int index = 0;
                  for (index = 0; index < values.Length; index++)
                  {
                      if (!IsDecimal(values[index]))
                      {
                          text = text + texts[index] + "必須為數(shù)字//n";
                      }
                  }
                  return text;
              }

              /// <summary>
              /// 簡體中文環(huán)境下檢查日期格式是否正確    接受 8位或10位的字串,其它位皆為 false
              /// </summary>
              /// <param name="sour">20060514 or 2005-05-14 等 8位或10位日期字串</param>
              /// <returns>Return Value</returns>
              public static bool RegDateCHS(string sour)
              {
                  string text = "";
                  if (sour.Length == 8)
                  {
                      text = sour.Substring(0, 4) + "-" + sour.Substring(4, 2) + "-" + sour.Substring(6, 2);
                  }
                  else if (sour.Length == 10)
                  {
                      text = sour.Substring(0, 4) + "-" + sour.Substring(5, 2) + "-" + sour.Substring(8, 2);
                  }
                  if (text == "")
                  {
                      return false;
                  }
                  try
                  {
                      Convert.ToDateTime(text);
                      return true;
                  }
                  catch
                  {
                      return false;
                  }
              }

              /// <summary>
              /// 是否是浮點數(shù)
              /// </summary>
              /// <param name="inputData">輸入字符串</param>
              /// <returns></returns>
              public static bool IsDecimal(string inputData)
              {
                  return RegDecimal.Match(inputData).Success;
              }
              /// <summary>
              /// 是否是浮點數(shù) 可帶正負號
              /// </summary>
              /// <param name="inputData"></param>
              /// <returns></returns>
              public static bool IsDecimalSign(string inputData)
              {
                  return RegDecimalSign.Match(inputData).Success;
              }

              /// <summary>
              /// 比較日期,必須為10位 2002/09/15格式
              /// </summary>
              /// <param name="Sour">待測試的日期字串    </param>
              /// <returns>ture or false</returns>
              private static bool RegDate(string Sour)
              {
                  Match match = new Regex(@"^([0-9]{4})//([0-9]{2})//([0-9]{2})$").Match(Sour);
                  bool flag = true;
                  if (match.Success)
                  {
                      int year = Convert.ToInt32(match.Groups[1].Value);
                      int month = Convert.ToInt32(match.Groups[2].Value);
                      int day = Convert.ToInt16(match.Groups[3].Value);
                      try
                      {
                          new DateTime(year, month, day);
                      }
                      catch
                      {
                          flag = false;
                      }
                      return flag;
                  }
                  return false;
              }
              /// <summary>
              /// 如果為空,return false, else return true;
              /// </summary>
              /// <param name="key"></param>
              /// <returns></returns>
              private static bool ToType(object key)
              {
                  if (key == null)
                  {
                      return false;
                  }
                  if (key == DBNull.Value)
                  {
                      return false;
                  }
                  if (key.ToString() == string.Empty)
                  {
                      return false;
                  }
                  if (key.ToString().Trim() == "")
                  {
                      return false;
                  }
                  if (key.ToString().ToLower() == " ")
                  {
                      return false;
                  }
                  return true;
              }
              /// <summary>
              /// 轉(zhuǎn)換為String
              /// </summary>
              /// <param name="key"></param>
              /// <returns></returns>
              public static string ToString(object key)
              {
                  if (ToType(key))
                  {
                      return key.ToString();
                  }
                  return "";
              }
         #region     // Fields

              private static Regex RegDecimal;
              private static Regex RegCHZN;
              private static Regex RegDecimalSign;
              private static Regex RegEmail;
              private static Regex RegNumber;
              private static Regex RegNumberSign;

              #endregion       
             
              #region     // Methods

              /// <summary>
              /// 構(gòu)造函數(shù)
              /// </summary>
              public CheckData()
              {
                  //
                  // TODO: 在此處添加構(gòu)造函數(shù)邏輯
                  //
              }

              static CheckData()
              {
                  RegNumber = new Regex("^[0-9]+$");
                  RegNumberSign = new Regex("^[+-]?[0-9]+$");
                  RegDecimal = new Regex("^[0-9]+[.]?[0-9]*$");
                  RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]*$");
                  RegEmail = new Regex(@"^[/w-]+@[/w-]+/.(com|cn|net|org|edu|mil|tv|biz|info)$");
                  RegCHZN = new Regex("[/u4e00-/u9fa5]");
              }

              /// <summary>
              /// 如果格式正確則返回空,否則返回錯誤信息適用于 2006/06/13 格式
              /// </summary>
              /// <param name="Sour">待測試的日期字串</param>
              /// <returns></returns>
              public static string CheckDate(string Sour)
              {
                  return CheckDate(Sour, true, true);
              }

              /// <summary>
              /// 如果格式正確則返回空,否則返回錯誤信息
              /// </summary>
              /// <param name="Sour">待測試的日期字串</param>
              /// <param name="IsUpToday">是否必須大于今天</param>
              /// <param name="IsNotNull">是否不可以為空值</param>
              /// <returns></returns>
              public static string CheckDate(string Sour, bool IsUpToday, bool IsNotNull)
              {
                  if (IsNotNull)
                  {
                      if (Sour == "")
                      {
                          return "日期不能為空";
                      }
                  }
                  else if (Sour == "")
                  {
                      return "";
                  }
                  if (IsUpToday && (Sour.CompareTo(DateTime.Now.ToString(@"yyyy//MM//dd")) < 0))
                  {
                      return "日期不能小于當前日期";
                  }
                  if (!RegDate(Sour))
                  {
                      return "日期格式不正確,應為【2002/09/06】";
                  }
                  return "";
              }
              /// <summary>
              /// 如果格式正確則返回空,否則返回錯誤信息
              /// </summary>
              /// <param name="Sour">待測試的日期字串</param>
              /// <param name="IsUpToday">是否必須大于今天</param>
              /// <returns></returns>
              public static string CheckDate(string Sour, bool IsUpToday)
              {
                  return CheckDate(Sour, IsUpToday, true);
              }
       

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約