字符串处理的几个方法

public sealed class CleanString
 {

  #region 数字字符串检查

  private static Regex _isNumber = new Regex("^[0-9]+$");
  
  //检查Request查询字符串的键值,是否是数字,最大长度限制
  // return a digit string based on input data
  // look first in the QueryString collection,
  //then in Form return string.Empty if not found or if non-digit
  public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen)
  {
   string retVal = string.Empty;
   if(inputKey != null && inputKey != string.Empty)
   {
    retVal = req.QueryString[inputKey];
    if(null == retVal)
     retVal = req.Form[inputKey];

    if(null != retVal)
    {
     retVal = CleanString.SqlText(retVal, maxLen);
     if(!IsNumber(retVal))
      retVal = string.Empty;
    }
   }

   if(retVal == null)
    retVal = string.Empty;

   return retVal;
  }


  //是否数字字符串
  public static bool IsNumber(string inputData)
  {
   Match m = _isNumber.Match(inputData);
   return m.Success;
  }
  //是否数字字符串
  public static bool IsNumberInt(string inputData)
  {
   try
   {
    int.Parse(inputData);
   }
   catch
   {
    return false;
   }
   return true;
  }
  //是否数字字符串
  public static bool IsNumberDecimal(string inputData)
  {
   try
   {
    decimal.Parse(inputData);
   }
   catch
   {
    return false;
   }
   return true;
  }

  
  //字符串编码
  public static string HtmlEncode(string inputData)
  {
   return HttpUtility.HtmlEncode(inputData);
  }


  public static void SetLabel(Label lbl, string txtInput)
  {
   lbl.Text = HtmlEncode(txtInput);
  }

  public static void SetLabel(Label lbl, object inputObj)
  {
   SetLabel(lbl, inputObj.ToString());
  }

  
  /// <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;
  }


  #endregion


  //字符串清理
  public static string InputText(string inputString, int maxLength)
  {   
   StringBuilder retVal = new StringBuilder();

   // 检查是否为空
   if ((inputString != null) && (inputString != String.Empty))
   {
    inputString = inputString.Trim();

    
    //检查长度
    if (inputString.Length > maxLength)
     inputString = inputString.Substring(0, maxLength);

    
    //替换危险字符
    for (int i = 0; i < inputString.Length; i++)
    {
     switch (inputString[i])
     {
      case '"':
       retVal.Append("&quot;");
       break;
      case '<':
       retVal.Append("&lt;");
       break;
      case '>':
       retVal.Append("&gt;");
       break;
      default:
       retVal.Append(inputString[i]);
       break;
     }
    }

    // 替换单引号
    retVal.Replace("'", " ");
   }

   return retVal.ToString();
   
  }
 }

posted @ 2006-12-29 10:42  '.Elvis.'  阅读(252)  评论(0编辑  收藏  举报