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