几个自己写的简单的字符处理方法(更新中)

using System;
 
namespace Public
{
 
     ///<summary>
     /// CharacterOperation 文字处理方法。
     ///</summary>
     public class CharacterOperation
     {
 
         #region构造函数
         ///<summary>
         ///构造函数(静态方法)
         ///</summary>
         public CharacterOperation()
         {
         }
         #endregion
 
         #region字符替换为HTML字符串
         ///<summary>
         ///字符替换方法(" , > , < , 回车 , 空格)
         /// </summary>
         ///<param name="str">待替换字符串</param>
         ///<returns>替换后的字符串</returns>
         public static string ReplaceCharHTML(string str)
         {
              str = str.Replace(((char)60).ToString(),"&lt;");
              str = str.Replace(((char)62).ToString(),"&gt;");
              str = str.Replace(((char)13).ToString(),"<br>");
              str = str.Replace(((char)32).ToString(),"&nbsp;");
              str = str.Replace(((char)34).ToString(),"&quot;");
             
              return str;
         }
         #endregion
 
         #region HTML字符串替换成原字符串(逆向方法)
         ///<summary>
         ///字符替换方法-逆向 (" , > , < , 回车 , 空格)
         /// </summary>
         ///<param name="str">待替换字符串</param>
         ///<returns>替换后的字符串</returns>
         public static string ReplaceCharText(string str)
         {
              str = str.Replace("<br>",((char)13).ToString());
              str = str.Replace("&lt;",((char)60).ToString());
              str = str.Replace("&gt;",((char)62).ToString());
              str = str.Replace("&nbsp;",((char)32).ToString());
              str = str.Replace("&quot;",((char)34).ToString());
             
              return str;
         }
         #endregion
 
         #region判断是否为数字字符串
         ///<summary>
         ///判断是否为数字字符串
         ///</summary>
         ///<param name="str">需验证的字符串</param>
         ///<returns>判断结果,符合条件为True,不符合条件为False</returns>
         public static bool IsNumeric(string str)
         {
              //判断是否为空
              if (str == null || str.Length==0)
              {
                   return false;
              }
              //循环检查每个字符
              foreach(char c in str)
              {
                   if (!Char.IsNumber(c))
                   {
                       return false;
                   }
              }
              return true;
         }
         #endregion
 
         #region static function ReplaceCharToShift 将字符串中小写字母转换为大写
         public static string ReplaceCharToShift(string str)
         {
              string C="";
              if(str!=null&&str.Length>0)
              {
                   foreach(char c in str)
                   {
                       if((short)c>=97 && (short)c<=122)
                       {
                            C+=(char)((short)c-32);
                       }
                       else
                       {
                            C+=c;
                       }
                   }
              }
 
              return C;
         }
         #endregion
 
         #region static function order Char 字符串内 字母排序
         public static string CharOrder(string str)
         {
              string[] str2=new string[str.Length];
              int i=0;
              foreach(char c in str)
              {
                   str2[i]=c.ToString();
                   i++;
              }
             
              Array.Sort(str2);
 
              str="";
              for(i=0;i<str2.Length;i++)
              {
                   str+=str2[i];
              }
             
              return str;
         }
         #endregion
     }
}
 
posted @ 2006-03-28 15:51  ZetaChow晓代码  阅读(224)  评论(0编辑  收藏  举报