Checking string in string

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

Why not use InStr?

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

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

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).

1 Like

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

1 Like

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:

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?

2 Likes

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

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

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

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

Does this do what you want?

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

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

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 )

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

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

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.

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.

You need to have the textarea set up and called TA_Result (as well as the textfields TF_Source and TF_Characters).

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

1 Like

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