过滤非法字符(脏字)的类

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.IO;
using  System.Web;
using  System.Collections;

namespace   ****** .WebHelper
{
    
public   class  FilterText
    {
        
string  FilePath  =   string .Empty;
        
public  FilterText()
        {
            FilePath 
=  HttpContext.Current.Server.MapPath( " ~/Config/ValidDict.txt " );//脏字文件的路径
        }
        
///   <summary>
        
///  读文件的内容
        
///   </summary>
        
///   <param name="url"> 物理路径 </param>
        
///   <returns></returns>
         public   string  ReadFilterText()
        {
            FileStream fsReadDefaultTemplate 
=   new  FileStream(FilePath, FileMode.Open, FileAccess.Read);
            StreamReader srReadDefaultTemplate 
=   new  StreamReader(fsReadDefaultTemplate, System.Text.Encoding.UTF8);
            
string  content  =  srReadDefaultTemplate.ReadToEnd();
            fsReadDefaultTemplate.Close();
            srReadDefaultTemplate.Close();
            
return  content;
        }

        
///   <summary>
        
///  写非法关键字到文件
        
///   </summary>
        
///   <param name="url"> 物理路径 </param>
        
///   <param name="content"> 内容 </param>
        
///   <returns></returns>
         public   void  WriteFilterText( string  content)
        {
            FileStream fs 
=   new  FileStream(FilePath, FileMode.Create, FileAccess.Write);
            StreamWriter sw 
=   new  StreamWriter(fs, System.Text.Encoding.UTF8);            
            sw.Write(content);
            sw.Close();
            fs.Close();
        }
        
///   <summary>
        
///  过滤非法字符,替换为*号
        
///   </summary>
        
///   <param name="TempStr"> 需要过滤的字符串 </param>
        
///   <returns></returns>
         public   string  FilterInfo( string  TempStr)
        {
            
if  (File.Exists(FilePath))
            {
                StreamReader objReader 
=   new  StreamReader(FilePath, System.Text.Encoding.UTF8);
                
string  sLine  =   "" ;
                ArrayList arrText 
=   new  ArrayList();
                
while  (sLine  !=   null )
                {
                    sLine 
=  objReader.ReadLine();
                    
if  (sLine  !=   null )
                        arrText.Add(sLine);
                }
                objReader.Close();
                
foreach  ( string  sOutput  in  arrText)
                {
                    
string [] strArr  =  sOutput.Split( new   char [] { ( char ) 10 , ( char ) 13  }, StringSplitOptions.RemoveEmptyEntries);

                    
for  ( int  i  =   0 ; i  <  strArr.Length; i ++ )
                    {
                        
string  temp  =   "" ;
                        
for  ( int  j  =   0 ; j  <  strArr[i].Length; j ++ )
                        { temp 
+=   " * " ; }
                        TempStr 
=  TempStr.Replace(strArr[i], temp);
                    }
                }
            }
            
return  TempStr;
        }
    }
}

你可能感兴趣的:(非法字符)