C\#키 워드 를 차단 하 는 실현 방법

txt 의 텍스트 를 새로 만 듭 니 다(코드 에서 이 텍스트 문서 경 로 를 읽 으 면 됩 니 다.이름 은 마음대로)
안에 있 는 내용 은 한 줄 이 하 나 를 대표 합 니 다.저 는 줄 에 따라 차단 할 키 워드 를 반복 해서 읽 기 때 문 입 니 다.그리고*번호 로 키 워드 를 차단 합 니 다.
예 를 들 어 포럼 에서'진흙 말'을 수출 하 는 것 은 비교적 민감 한 화제,이름 과 관련 되 고 홍보 가 비교적 뜨 거 운 사이트 에서 모두 허용 되 지 않 기 때문에 여 기 는'**'만 표 시 됩 니 다  여기 코드 아래 에 제 가 나 왔 습 니 다.주석 이 상세 합 니 다.모 르 는 것 은 댓 글로 저 에 게 물 어보 세 요.블 로 거들 이 매일 조금씩 발전 하 기 를 바 랍 니 다. 

  /// <summary>
        /// ( , "***" )
        /// </summary>
        /// <param name="strText"> </param>
        /// <returns> </returns>
        public static string CheckKeyword(string strText)
        {
            IList<string> list = new List<string>();     //
            string strpath = System.Web.HttpContext.Current.Server.MapPath("function/keyword.txt");   //
            int a =strpath.LastIndexOf("IFSns");   
            int b =strpath.IndexOf("function");
            string m = strpath.Substring(a+5, b - a - 6);
            string PathTxt = strpath.Replace(m, "");    //
            FileStream fs = new FileStream(PathTxt, FileMode.Open, FileAccess.Read);  // txt ,
            StreamReader reader = new StreamReader(fs, Encoding.Default); //
            string strLine = reader.ReadLine();
            while (strLine!=null&&strLine.Length != 0)    //
            {
                list.Add(strLine.Trim().Replace(" ",""));    // , , string
                strLine = reader.ReadLine();   // ,
            }
            fs.Close();  //
            foreach (string str in list)    //
            {
                if (strText.Contains(str))   
                {
                    int lg = str.Length;
                    string sg = "";
                    for (int i = 0; i < lg; i++)
                    {
                        sg+="*";
                    }
                    strText = strText.Replace(str, sg);  // txt , "***"
                }
            }
            return strText;
        }
 

좋은 웹페이지 즐겨찾기