字符串处理

一:最笨的一种
public static string StringProcess(System.Web.UI.WebControls.TextBox tb,int MaxLength)
  {
   string returnStr="return";
   StringBuilder sb = new StringBuilder();
   string str = tb.Text.Trim();
   if((str=="")&&(str==String.Empty))
   {
    returnStr="null";
   }
   else
   {
    if(str.Length>MaxLength)
    {
     str=str.Substring(0,MaxLength);
    }
    for(int i=0;i    {
     #region  swithc块
     if(returnStr=="warning")
     {
      return returnStr;
     }
     switch(str[i])
     {
      case '"':
       returnStr="warning";
       break;
      case '@':
       returnStr="warning";
       break;
      case '!':
       returnStr="warning";
       break;
      case '<':
       returnStr="warning";
       break;
      case '>':
       returnStr="warning";
       break;
      case '/':
       returnStr="warning";
       break;
      case '&':
       returnStr="warning";
       break;
      case '*':
       returnStr="warning";
       break;
      case '(':
       returnStr="warning";
       break;
      case ')':
       returnStr="warning";
       break;
      default :
       sb.Append(str[i]);
       break;
     }
     
     #endregion     
    }
    if(returnStr=="warning")
    {
     return returnStr;
    }
    else
    {
     returnStr=sb.ToString();
    }
    
   }
   return returnStr;
  }

二:进化了的
private void Page_Load(object sender, System.EventArgs e)
  {
   string testStr = "abcd@";
   char[] specChar=new char[]{'@','<','#'};
   for(int i= 0;i   {
    if (testStr.IndexOf(specChar[i]) != -1)
    {
     Response.Write("有特殊字符:“" + specChar[0].ToString() + "”");
     break;
    }
   }
    
  }
三:高级动物了
char[] specChar=new char[]{'@','<','#'};
if  testStr.IndexOfAny( specChar)>-1
四:还高一点
string strTest="d@@ddddd";
   if(System.Text.RegularExpressions.Regex.IsMatch(strTest,@"[@%^&*]"))
   {
    Response.Write("有特殊字符");
   }
五:不相上下
<SCRIPT LANGUAGE="JavaScript">
<!--
if('sdfsd#'.match(/[@#<]/) != null )
 alert("there are some special characters")
//-->
</SCRIPT>
posted on 2005-11-18 13:38  泽来  阅读(262)  评论(0编辑  收藏  举报