zoukankan      html  css  js  c++  java
  • 字符串处理

     public sealed class CleanString
     {
      /// <summary>
      /// 输入替换
      /// </summary>
      /// <param name="inputString"></param>
      /// <param name="maxLength"></param>
      /// <returns></returns>
      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;
          case ' ':
           retVal.Append("&nbsp;");
           break;
          case (char)13:
           retVal.Append("<br />");
           break;
          default:
           retVal.Append(inputString[i]);
           break;
         }
        }

        retVal.Replace("'", "");
       }

       return retVal.ToString();   
      }

      /// <summary>
      /// 输出替换
      /// </summary>
      /// <param name="inputString"></param>
      /// <returns></returns>
      public static string OutputText(string inputString)
      {   
       StringBuilder retVal = new StringBuilder(inputString.Trim());

       if ((inputString != null) && (inputString != String.Empty))
       {
        retVal.Replace("&quot;","\"");
        retVal.Replace("&lt;","<");
        retVal.Replace("&gt;",">");
        retVal.Replace("&amp;","&");
        retVal.Replace("&nbsp;"," ");
       }

       return retVal.ToString();   
      }

      /// <summary>
      /// 截取固定长度字符串
      /// </summary>
      /// <param name="inputString"></param>
      /// <param name="len"></param>
      /// <returns></returns>
      public static string CutString(string inputString,int len)
      {
       int tempLen = 0;
       string tempString = "";

       ASCIIEncoding ascii = new ASCIIEncoding();
       byte[] s = ascii.GetBytes(inputString);
       
       for(int i = 0;i < s.Length;i++)
       {
        if((int)s[i] == 63)
        {
         tempLen += 2;
        }
        else
        {
         tempLen += 1;
        }

        try
        {
         tempString += inputString.Substring(i,1);
        }
        catch
        {
         break;
        }

        if(tempLen>len)
        {
         break;
        }
       }

       //如果截过则加上半个省略号
       byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
       if(mybyte.Length>len)
       {
        tempString += "…";
       }

       return tempString;
      }
      
      //kill掉标签
      public static string StripHT(string strHtml)
      {
       Regex regex=new Regex("<.+?>",RegexOptions.IgnoreCase);
       string strOutput=regex.Replace(strHtml,"");
       return strOutput;
      }

     }

  • 相关阅读:
    Windows Store App 主题动画
    Windows Store App 过渡动画
    Windows Store App 控件动画
    Windows Store App 近期访问列表
    Windows Store App 文件选取器
    Windows Store App 访问应用内部文件
    Windows Store App 用户库文件分组
    Windows Store App 获取文件及文件夹列表
    Windows Store App 用户库文件夹操作
    Windows Store App 用户库文件操作
  • 原文地址:https://www.cnblogs.com/zyosingan/p/1015338.html
Copyright © 2011-2022 走看看