Extract words from a string and verify whether or

2019-09-11 10:33发布

问题:

I am trying to create a web-method to check whether a string contains a word which is in a list of 'not allowed' words. This list will be updated from time to time.

<System.Web.Services.WebMethod()> _
Public Function checkword(ByVal Id As String) As String
  Dim returnValue As String = String.Empty
  Dim s As String = Id

  ' Split string based on spaces
  Dim words As String() = s.Split(New Char() {" "c})

  Dim rer As Integer
  Dim word As String
  For Each word In words

    'call class file
    Dim oscar As New webfunctions

    Try
      'call function to check whether this word exists in database or not, 
       rer = oscar.chkword(word)
      If rer > 0 Then
        returnValue = "a"
      Else
        returnValue = "exists"
      End If

    Catch ex As Exception
      returnValue = ex.Message
    End Try
  Next
  Return returnValue
End Function

Something is wrong with this function. It verifies the first word of string. After that it doesn't return anything. Please, any suggestions?

回答1:

You are looping through all of your words and only returning the results from the last word.

It's not very clear from your code how it's suppose to work because you are returning "a", "exists" or an error message.

This code breaks out of the loop (and function) if it finds a "disallowed" word (which one apparently doesn't matter to you) or if it throws an error (which also isn't clear why it would throw an error):

For Each word In words

  'call class file
  Dim oscar As New webfunctions

  Try

  'call function to check whether this word exists in database or not, 
  rer = oscar.chkword(word)
  If rer > 0 Then
    returnValue = "a"
  Else
    return "exists"
  End If

  Catch ex As Exception
    return ex.Message
  End Try
Next
Return returnValue

Per your comment and your updated code, I would probably re-work your function to return a dictionary of results for each word, since that seems to be what you are looking for:

<System.Web.Services.WebMethod()> _
Public Function CheckWords(ByVal sentence As String) As Dictionary(Of String, String)
  Dim wordDictionary As New Dictionary(Of String, String)

  For Each word As String In sentence.Split(" ")
    Dim wordResult As String
    Try
      Dim oscar As New webfunctions
      If oscar.chkword(word) > 0 Then
        wordResult = "a"
      Else
        wordResult = "exists"
      End If
    Catch ex As Exception
      wordResult = ex.Message
    End Try

    If Not wordDictionary.ContainsKey(word) Then
      wordDictionary.Add(word, wordResult)
    End If
  Next

  Return wordDictionary
End Function


回答2:

i used one stringbuilder and its solved now

<System.Web.Services.WebMethod()> _
Public Function checkword(ByVal Id As String) As String
  Dim returnValue As String = String.Empty
  Dim s As String = Id
 dim sb as new stringBuilder
  ' Split string based on spaces
  Dim words As String() = s.Split(New Char() {" "c})

  Dim rer As Integer
  Dim word As String
  For Each word In words

    'call class file
    Dim oscar As New webfunctions

    Try
      'call function to check whether this word exists in database or not, 
       rer = oscar.chkword(word)
      If rer > 0 Then
        returnValue = "a"
      Else
       sb.append(word & ",")
      End If

    Catch ex As Exception
      returnValue = ex.Message
    End Try
  Next
  Return sb.tostring + "exists"
End Function