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

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 " );//脏字文件的路径
        }
        
///  
        
///  读文件的内容
        
///  

        
///   物理路径
        
///  
         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;
        }

        
///  
        
///  写非法关键字到文件
        
///  

        
///   物理路径
        
///   内容
        
///  
         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();
        }
        
///  
        
///  过滤非法字符,替换为*号
        
///  

        
///   需要过滤的字符串
        
///  
         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;
        }
    }
}

转载于:https://www.cnblogs.com/skyshenwei/archive/2010/06/04/1751544.html

你可能感兴趣的:(过滤非法字符(脏字)的类)