How to check whether a user string contains any forbidden words from a list in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic 
Imports System.Text.RegularExpressions

Module Module1

    ' Lookup, case-insensitive
    Private ReadOnly Forbidden As New HashSet(Of String)(
        {"badword", "evil", "kill", "nasty", "terrible"},
        StringComparer.OrdinalIgnoreCase
    )

    Function ContainsForbidden(input As String) As Boolean
        ' Normalize: lowercase + split on non-alphanumeric
        Dim words = Regex.Split(input.ToLower(), "[^a-z0-9]+")

        For Each w In words
            If Forbidden.Contains(w) Then
                Return True
            End If
        Next

        Return False
    End Function

    Sub Main()
        Dim s = "This text contains a badword inside"

        If ContainsForbidden(s) Then
            Console.WriteLine("Forbidden word detected")
        Else
            Console.WriteLine("No forbidden words found")
        End If
    End Sub

End Module


' run:
'
' Forbidden word detected
'

 



answered Dec 26, 2025 by avibootz

Related questions

...