Use 'Contains' with a list to make a replacement?

U

unba1300

Guest
Is it possible to use the 'Contains' keyword with a list of words? I need to do something like the following:
B4X:
varRelation = "his relationship's upper body"
varRelation.Replace("relationship", varRelationship)
If varRelation.Contains("mother", "sister", "grandmother", "girlfriend") Then
    varRelation.Replace("upper body", "breasts")
Else If varRelation.Contains("father", "brother", "grandfather", "boyfriend") Then
    varRelation.Replace("upper body", "chest")
End If
Thanks.
 

Mahares

Expert
Licensed User
Longtime User
I think you need sonmething like this:

B4X:
     Dim varRelation As String
      varRelation="His father's upper body shows unprecedented athleticism"

      If varRelation.Contains("mother") OR varRelation.Contains("sister") _
      OR varRelation.Contains("grandmother") OR varRelation.Contains("girlfriend") Then
          varRelation=varRelation.Replace("upper body", "breasts")
      Else If varRelation.Contains("father") OR varRelation.Contains("brother") _
      OR varRelation.Contains("grandfather") OR varRelation.Contains("boyfriend") Then            
          varRelation=varRelation.Replace("upper body", "chest")
      End If
      Msgbox(varRelation,"")
The result will be: His father's chest shows unprecedented athleticism. Would this work for you or is it too simplistic?
 
Upvote 0
U

unba1300

Guest
Thanks, but I was trying to avoid all the OR statements since the list of gender words is actually more than twice as long than in my example.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
B4X:
Dim ListOfWords As List
ListOfWords = File.ReadList(...)
If ListOfWords.IndexOf("word1") > -1 Then

this solution is really good, it saves me lots of for...next to search for a specific item !
really great erel
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
mistake, its not what i need, this return the position of the item but i cannot search only a contains text in this item i need to put the correct item text
is it possible to search with indexof a contains text of the item?

like my item is: USD|0.9252
and i would like to get the index of this item by searching for USD
without a For..next loop!
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
what about something like
B4X:
Sub searchTest
    Dim src As String = "his friend was near his brother"
    Dim terms() As String = Array As String ("mother","father","sister","brother")
    Log(contains(src,terms))
    Log(contains(src,Array As String("friend")))
End Sub
Sub contains(src As String,terms() As String) As Int
    For Each item As String In terms
        If src.Contains(item) Then
            Return src.IndexOf(item)
        End If
    Next
    Return -1
End Sub
you could change the int return to boolean and just have true (it does contain) or false ( does not contain) the terms.
 
Last edited:
Upvote 0
Top