Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

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)