Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

ASP.NET: Filter words

From Wiki

Jump to: navigation, search

Summary: An example of how we can filter certain words in a string and mask them by replacing them with an asterisk.

  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.     ' Declarations  
  3.     Dim arrWords As New ArrayList
  4.     Dim strArticle As String = "This is one sample string that three people wrote"
  5.  
  6.     ' Populate the array list with the words we want to replace  
  7.     arrWords.Add("one")
  8.     arrWords.Add("two")
  9.     arrWords.Add("three")
  10.  
  11.     ' Loop through the array list and replace each found word with *'s  
  12.     Dim IEnum As IEnumerator = arrWords.GetEnumerator
  13.     While IEnum.MoveNext
  14.         strArticle = strArticle.ToLower.Replace(IEnum.Current, New String("*", IEnum.Current.ToString.Length))
  15.     End While
  16.  
  17.     ' Show the resulting string  
  18.     Label1.Text = strArticle
  19. End Sub

Output:

  1. This is *** sample string that ***** people wrote


This Hack is part of the ASP.NET Hacks collection

466 Rating: 2.0/5 (5 votes cast)