Android Question String Contain function and an array of items.

Sub7

Active Member
Licensed User
Longtime User
Hello!
i need to search for multiple words into a string, a sort of bad words filter.
Instead of making a huge list of
B4X:
If
thestring.contains("this phrase is banned") OR thestring.contains("dog") OR thestring.contains("cat") Then
'..
and so on.
Is there any other way to accomplish this task like pass an array of items without all that OR conditions? the list is huge.
in php i would create an array and use strpos function for matching.

Thank you
 

DonManfred

Expert
Licensed User
Longtime User
I found now: "spigot" (I learned a new word ... until tomorrow, at least :D)

If you were Kelly Bundy than you´ll lost the first word out of your memory to make room for one new word :D
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
I need a more efficent way to search for one or more words inside a phrase.
banned word: dog, flea

Thedesolatedsoul solutions only works for exact match.
eg: string = "dog" > works.
string = "my dog has a flea" does not works.

thx
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I need a more efficent way to search for one or more words inside a phrase.
banned word: dog, flea

Thedesolatedsoul solutions only works for exact match.
eg: string = "dog" > works.
string = "my dog has a flea" does not works.

Just rewrite @thedesolatesoul s sub a bit....

B4X:
Sub CountBanned(pString As String, pBannedWordList As List) As Int
   Dim count As Int
   For Each BannedWord As String In pBannedWordList
     If pString.ToUpperCase.Contains(BannedWord.ToUpperCase) Then
       count = count +1
  End If
  Next
  Return count
End Sub

and then use it

B4X:
Dim blist As List
blist.Initialize2(Array As String("Dog","Pig", "FLEA"))
Log(CountBanned( "my dog has a flea",blist)&" bad words found")

2 bad words found
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
Just rewrite @thedesolatesoul s sub a bit....

B4X:
Sub CountBanned(pString As String, pBannedWordList As List) As Int
   Dim count As Int
   For Each BannedWord As String In pBannedWordList
     If pString.ToUpperCase.Contains(BannedWord.ToUpperCase) Then
       count = count +1
  End If
  Next
  Return count
End Sub

and then use it

B4X:
Dim blist As List
blist.Initialize2(Array As String("Dog","Pig", "FLEA"))
Log(CountBanned( "my dog has a flea",blist)&" bad words found")

Good!! thx!
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I need a more efficent way to search for one or more words inside a phrase.
banned word: dog, flea

Thedesolatedsoul solutions only works for exact match.
eg: string = "dog" > works.
string = "my dog has a flea" does not works.

thx


The The desolatesoul :) solution works well for your example; unless you do not have the need to know how many words not allowed are contained.
"dog" is sufficient to ensure that the sentence is to ban.

Only missing "As Boolean" in the definition of the function.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Regex is ideal, in these cases, but I do not know it (it's a real language and I do not want to study it).

If necessary, you can check the previous and the next character when found a word: if one of them is an alphabetic character, probably the word is contained in another word (spigot).

Instead of:
pString.Contains(BannedWord)

Pos = pString.IndexOf(BannedWord)
If pos = -1 then ' not found
else
Prev = BannedWord.SubString2(pos-1,pos)
Nxt = BannedWord.SubString2(pos+1,pos+2)
If IsLetter(Prev) OR IsLetter(Nxt) then
else
etc.


Sub IsLetter(L as string) as boolean
' here you could use Asc()
return ("ABCDEFGHIJKLMNOPQRSTUVWXYZ".Contains(L.ToUpperCase) > -1)
End sub


Do not forget that in the chats, users come up with some tricks to get around this.

Like: "d o g" or "do g" or "d_o_g" or "FU" :D
 
Last edited:
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
To use Regex you will have to use Matcher to extract all the words and then go over all the words.

B4X:
Dim text, pattern As String
text = "This is an interesting sentence with two words: dog and dogma."
pattern = "\w+" 'one word
Dim Matcher1 As Matcher
Matcher1 = Regex.Matcher(pattern, text)
Do While Matcher1.Find
IsBanned(Matcher1.Match, BannedList)
Loop
or use the CountBanned or other variant.
 
Upvote 0
Top