# Checking string in string

**URL:** <https://forum.xojo.com/t/checking-string-in-string/61746>\
**Category:** Getting Started\
**Created:** [March 3, 2021, 2:08am UTC](https://forum.xojo.com/t/checking-string-in-string/61746 "2021-03-03T02:08:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tibor\_Nagy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@Tibor\_Nagy](https://forum.xojo.com/u/Tibor_Nagy)\
**Post date:** [March 3, 2021, 2:08am UTC](https://forum.xojo.com/t/checking-string-in-string/61746/1 "2021-03-03T02:08:54Z")

</div>

Hi  
I have a string: string1=“123,45”, and I would like to check if that string contains a piece of an another string: string2=",./" .  
I wrote this short program and it can find part of string2 but it is finding it 3 times and not only 1 time. Can somebody help please?  
Thanks  
TN

var string1,string2,result As String  
Var i,j,count,count1 As integer

string1=“123,45”  
string2=",./"

count=len(string1)

For i=1 To count  
For j=1 To 3  
result=Mid(string2,j,1)  
count1=string1.IndexOf(i,result)  
if count1 \> 0 then  
MessageBox(result)  
end if  
Next j  
Next i

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [March 3, 2021, 3:12am UTC](https://forum.xojo.com/t/checking-string-in-string/61746/2 "2021-03-03T03:12:49Z")

</div>

Why not use InStr?

---

<div class="post-metadata">

**Author:** ![Tibor\_Nagy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@Tibor\_Nagy](https://forum.xojo.com/u/Tibor_Nagy)\
**Post date:** [March 3, 2021, 3:24am UTC](https://forum.xojo.com/t/checking-string-in-string/61746/3 "2021-03-03T03:24:52Z")

</div>

Hi  
InStr was **deprecated** in version **2019r2**. This is way i didn’t use that. The  
replacement is String.IndexOf and the result is the same.  
Thanks

---

<div class="post-metadata">

**Author:** ![Tibor\_Nagy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@Tibor\_Nagy](https://forum.xojo.com/u/Tibor_Nagy)\
**Post date:** [March 3, 2021, 3:40am UTC](https://forum.xojo.com/t/checking-string-in-string/61746/4 "2021-03-03T03:40:40Z")

</div>

I used your suggestion and previously i hade 3 results and now i have 6.  
Thanks  
var string1,string2,result As String  
Var i,j,count,count1 As integer

string1=“123,45”  
string2=",./"

count=len(string1)

For i=1 To count  
For j=1 To 3  
result=Mid(string2,j,1)  
count1=instr(string1,result)  
if count1 \> 0 then  
MessageBox(result)  
end if  
Next j  
Next i

---

<div class="post-metadata">

**Author:** ![Bill\_Gookin](https://forum.xojo.com/user_avatar/forum.xojo.com/bill_gookin/32/172_2.png) [@Bill\_Gookin](https://forum.xojo.com/u/Bill_Gookin)\
**Post date:** [March 3, 2021, 4:03am UTC](https://forum.xojo.com/t/checking-string-in-string/61746/5 "2021-03-03T04:03:21Z")

</div>

Why do you have the “for i” loop in the more recent code? It isn’t doing anything. Remove it and see if you get the correct result (I did).

---

<div class="post-metadata">

**Author:** ![Tibor\_Nagy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@Tibor\_Nagy](https://forum.xojo.com/u/Tibor_Nagy)\
**Post date:** [March 3, 2021, 4:11am UTC](https://forum.xojo.com/t/checking-string-in-string/61746/6 "2021-03-03T04:11:31Z")

</div>

Hi  
I removed the “for i” loop and it works.  
Thanks for your help.  
TN

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [March 3, 2021, 4:21am UTC](https://forum.xojo.com/t/checking-string-in-string/61746/7 "2021-03-03T04:21:31Z")

</div>

> [@Tibor\_Nagy](#):
>
> result=Mid(string2,j,1)

And don’t call something that isn’t the result “result”. Code becomes MUCH more readable and easier to understand if you use correct language:

```auto
var SourceString, CharactersToSearchFor, CharToSearchFor As String
Var i,j, NumberOfCharactersToSearchFor, FoundAtIndex As integer

SourceString = "123,45"
CharactersToSearchFor = ",./"

NumberOfCharactersToSearchFor = len(CharactersToSearchFor)

For j = 1 To NumberOfCharactersToSearchFor
  CharToSearchFor = Mid( CharactersToSearchFor, j, 1 )
  FoundAtIndex = SourceString.IndexOf( i, CharToSearchFor )
  if FoundAtIndex > 0 then
    MessageBox( CharToSearchFor )
  end if
Next j

```

Isn’t that MUCH easier to follow?

---

<div class="post-metadata">

**Author:** ![Tibor\_Nagy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@Tibor\_Nagy](https://forum.xojo.com/u/Tibor_Nagy)\
**Post date:** [March 3, 2021, 4:22am UTC](https://forum.xojo.com/t/checking-string-in-string/61746/8 "2021-03-03T04:22:32Z")

</div>

I modified a little bit the program to check out and it did not work.  
when i have the same character in string2 i am not getting the result.  
The reason why i wanted to use two loops because in the first loop in 1 character  
the second loop is checking every character in string2 and going into the second character and so on. When i have one loop only checking one time.  
Thanks

var string1,string2,result As String  
Var i,j,count,count1 As integer

string1=“123,4533//’”  
string2=",./’"

count=len(string1)

For j=1 To 4  
result=Mid(string2,j,1)  
count1=instr(string1,result)  
if count1 \> 0 then  
MessageBox(result)  
end if  
Next j

---

<div class="post-metadata">

**Author:** ![Tibor\_Nagy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@Tibor\_Nagy](https://forum.xojo.com/u/Tibor_Nagy)\
**Post date:** [March 3, 2021, 4:31am UTC](https://forum.xojo.com/t/checking-string-in-string/61746/9 "2021-03-03T04:31:41Z")

</div>

> [@Markus\_Winter](#):
>
> ```auto
> var SourceString, CharactersToSearchFor, CharToSearchFor As String
> Var i,j, NumberOfCharactersToSearchFor, FoundAtIndex As integer
> 
> SourceString = “123,45”
> CharactersToSearchFor = ",./"
> 
> NumberOfCharactersToSearchFor = len(CharactersToSearchFor)
> 
> For j = 1 To NumberOfCharactersToSearchFor
> CharToSearchFor = Mid( CharactersToSearchFor, j, 1 )
> FoundAtIndex = SourceString.IndexOf( i, CharToSearchFor )
> if FoundAtIndex > 0 then
> MessageBox( CharToSearchFor )
> end if
> Next j
> 
> ```

Hi  
I checked your program and i got error at SourceString = “123,45” This item is not exist. The second error is Type mismatch . Expected string , but got int32.  
Thanks

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [March 3, 2021, 5:01am UTC](https://forum.xojo.com/t/checking-string-in-string/61746/10 "2021-03-03T05:01:01Z")

</div>

Copy Paste mangled up the quote characters (should be straight quotes, not curly). Project for you:

> **[ForTiborNagy.xojo\_binary\_project.zip](https://www.dropbox.com/s/k0je3fikeq62ty8/ForTiborNagy.xojo_binary_project.zip?dl=0)**
>
> Shared with Dropbox

---

<div class="post-metadata">

**Author:** ![Tibor\_Nagy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@Tibor\_Nagy](https://forum.xojo.com/u/Tibor_Nagy)\
**Post date:** [March 3, 2021, 5:14am UTC](https://forum.xojo.com/t/checking-string-in-string/61746/11 "2021-03-03T05:14:17Z")

</div>

Hi  
Thanks for your help. If i have this string1=“123,45//” the program can’t fined that because it checks only one time. I would like to get result when i have the characters more then one or two or any times in string1.  
Thanks

---

<div class="post-metadata">

**Author:** ![Robert\_Weaver](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/919ad9/32.png) [@Robert\_Weaver](https://forum.xojo.com/u/Robert_Weaver)\
**Post date:** [March 3, 2021, 6:00am UTC](https://forum.xojo.com/t/checking-string-in-string/61746/12 "2021-03-03T06:00:30Z")

</div>

Does this do what you want?

```auto
var string1,string2,result As String
Var i,j,count,count1 As integer
string1=“123,45”
string2=",./"

count=len(string1)

For i=1 To count
  result=Mid(string2,i,1)
  count1=string1.countfields(result)-1
  if count1 > 0 then
    MessageBox(result)
  end if
Next i
```

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [March 3, 2021, 7:19am UTC](https://forum.xojo.com/t/checking-string-in-string/61746/13 "2021-03-03T07:19:07Z")

</div>

Same link, better project. Mid etc are very slow, so I split the strings into arrays and compare the different array entries (much faster).

```auto
Var SourceString, CharactersToSearchFor, CharToSearchFor, ResultsArray() As String
Var i, j As Integer

SourceString = TF_Source.Text
CharactersToSearchFor = TF_Characters.Text
TA_Result.Text = ""

Var SourceArray() As String = SourceString.Split("")
Var CharArray() As String = CharactersToSearchFor.Split("")

For i = CharArray.FirstIndex To CharArray.LastIndex
  CharToSearchFor = CharArray(i)
  
  For j = SourceArray.FirstIndex To SourceArray.LastIndex
    If CharToSearchFor = SourceArray(j) Then
      ResultsArray.Add Str(j+1) + ": " + CharToSearchFor
    End If
    
  Next j
  
Next i

TA_Result.Text = Join( ResultsArray, EndOfLine )

```

[![](https://forum.xojo.com/uploads/default/original/2X/9/9a0a1cc1d126f4264bef414c185b915f9cd3502f.png) ](https://dl.dropboxusercontent.com/s/kvcy8r8t9y762fo/screenshot_295.png?dl=0)

---

<div class="post-metadata">

**Author:** ![Tibor\_Nagy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@Tibor\_Nagy](https://forum.xojo.com/u/Tibor_Nagy)\
**Post date:** [March 3, 2021, 1:28pm UTC](https://forum.xojo.com/t/checking-string-in-string/61746/14 "2021-03-03T13:28:05Z")

</div>

Hi  
Thanks for your help Robert. Markus solution what I was looking for.  
TN

---

<div class="post-metadata">

**Author:** ![Tibor\_Nagy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@Tibor\_Nagy](https://forum.xojo.com/u/Tibor_Nagy)\
**Post date:** [March 3, 2021, 1:48pm UTC](https://forum.xojo.com/t/checking-string-in-string/61746/15 "2021-03-03T13:48:09Z")

</div>

Hi  
Thanks Markus. This is what i would like to do. It is not working for me because i am getting 2 error messages at TA\_Result.Text = “” and TA\_Result.Text = Join( ResultsArray, EndOfLine ) . I am using Win 10. Maybe this is the problem why your solution is not working on my laptop. I will try to work with your idea. If you have the solution for that error please let me know.  
Thanks  
TN

---

<div class="post-metadata">

**Author:** ![Bill\_Gookin](https://forum.xojo.com/user_avatar/forum.xojo.com/bill_gookin/32/172_2.png) [@Bill\_Gookin](https://forum.xojo.com/u/Bill_Gookin)\
**Post date:** [March 3, 2021, 3:39pm UTC](https://forum.xojo.com/t/checking-string-in-string/61746/16 "2021-03-03T15:39:32Z")

</div>

Check to make sure the quotes are correct in the TA\_Result.Text = “” line. They may have copied in again as curly quotes instead of straight quotes.

---

<div class="post-metadata">

**Author:** ![Tibor\_Nagy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@Tibor\_Nagy](https://forum.xojo.com/u/Tibor_Nagy)\
**Post date:** [March 3, 2021, 3:50pm UTC](https://forum.xojo.com/t/checking-string-in-string/61746/17 "2021-03-03T15:50:29Z")

</div>

I checked and the quotes are correct TA\_Result.Text = “”. I don’t see any declaration of TA\_Result in Markus program. I am getting the “This item does not exist” message.

---

<div class="post-metadata">

**Author:** ![Bill\_Gookin](https://forum.xojo.com/user_avatar/forum.xojo.com/bill_gookin/32/172_2.png) [@Bill\_Gookin](https://forum.xojo.com/u/Bill_Gookin)\
**Post date:** [March 3, 2021, 3:57pm UTC](https://forum.xojo.com/t/checking-string-in-string/61746/18 "2021-03-03T15:57:25Z")

</div>

You need to have the textarea set up and called TA\_Result (as well as the textfields TF\_Source and TF\_Characters).

---

<div class="post-metadata">

**Author:** ![Tibor\_Nagy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@Tibor\_Nagy](https://forum.xojo.com/u/Tibor_Nagy)\
**Post date:** [March 3, 2021, 4:26pm UTC](https://forum.xojo.com/t/checking-string-in-string/61746/19 "2021-03-03T16:26:02Z")

</div>

Thanks Bill. I renamed a text area and it works.

---

<div class="post-metadata">

**Author:** ![Tibor\_Nagy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@Tibor\_Nagy](https://forum.xojo.com/u/Tibor_Nagy)\
**Post date:** [March 4, 2021, 3:50am UTC](https://forum.xojo.com/t/checking-string-in-string/61746/20 "2021-03-04T03:50:08Z")

</div>

Thanks every body for helping me out with this problem!  
TN
