-
Sep 1st, 2014, 09:14 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Instr function
Hi All,
I need to evaluate if a string that contains numbers has a certain number. For this purpose I thought that I can use the Instr function but it seems the approach is wrong. My example looks like below :
Code:
Dim S As String, p As Long
S = "1825, 8253"
p = InStr(1, s, "825")
Either I set the comparison argument as binary or as text each time the function returns "2" while I expected to have "0". That happens because Instr does read each substring sequence inside S string...
Is it possible to compare string numbers as if I compare numbers only? I would like to avoid any loop for this task. Thank you.
Last edited by Daniel Duta; Sep 1st, 2014 at 09:17 AM.
-
Sep 1st, 2014, 09:53 AM
#2
Re: Instr function
The result is correct. "825" is found at position 2 in the string "1825, 8253"
What kind of "magic" do you need?
-
Sep 1st, 2014, 09:56 AM
#3
Re: Instr function
Some code from a library to work with "multivalue" strings.
In your case the Separator is ", "
Code:
Public Function mvLocate(sString As String, sSearch As String, sSeparator As String) As Long
Dim lFieldNo As Long
Dim lPos1 As Long, lPos2 As Long, lStart As Long
If Len(sString) = 0 Or Len(sSearch) = 0 Then
Exit Function
End If
lPos2 = 1
lPos1 = 1
lStart = 1
Do While lPos1 > 0
lFieldNo = lFieldNo + 1
lPos1 = InStr(lPos2, sString, sSeparator, vbBinaryCompare)
lStart = lPos2
If lPos1 = 0 Then
If Mid$(sString, lStart) = sSearch Then
mvLocate = lFieldNo
Exit Do
End If
Else
If Mid$(sString, lStart, lPos1 - lStart) = sSearch Then
mvLocate = lFieldNo
Exit Do
End If
End If
lPos2 = lPos1 + 1
Loop
End Function
-
Sep 1st, 2014, 10:07 AM
#4
Re: Instr function
Function you seem to need would to be a mini-wordprocessor of sorts
-
Sep 1st, 2014, 10:10 AM
#5
Re: Instr function
Originally Posted by LaVolpe
Function you seem to need would to be a mini-wordprocessor of sorts
A Regular Expression engine might qualify as such.
-
Sep 1st, 2014, 10:16 AM
#6
Thread Starter
Hyperactive Member
Re: Instr function
Hi Arno,
Thank you for reply. Your function is not very different than other based on Split function, which is even smaller. After that you can loop the array and if n1=n2 than exit for/sub. The idea is it involves a loop and I wanted to avoid this.
The result is correct. "825" is found at position 2 in the string "1825, 8253"
Unfortunately, you are right. I wanted to get "0" for that comparison because "825" <> "1825" but I think it is not possible to do this using Instr function as long as it returns the position of a substring in a string.
-
Sep 1st, 2014, 10:35 AM
#7
Re: Instr function
If you only need to know whether it is present or not:
Code:
Option Explicit
Function Present(ByVal Numbers As String, ByVal Target As String) As Boolean
Present = " " & Numbers & " " Like "*[ ,]" & Target & "[ ,]*"
End Function
Private Sub Form_Load()
Dim Numbers As String
Numbers = "1825, 8253"
MsgBox Present(Numbers, "825")
MsgBox Present(Numbers, "8253")
Unload Me
End Sub
-
Sep 1st, 2014, 11:12 AM
#8
Thread Starter
Hyperactive Member
Re: Instr function
Hello Dille,
I was about to start a function based on the native Split function when I read your post. It is exactly what I needed. Only to check the presence of a string number in other. So it is not a mini-wordprocessor but a simple, smart and beautiful solution. I think you had the best maths grades at the school.
Thank you for this solution. It is a good example of ingenuity.
-
Sep 1st, 2014, 11:15 AM
#9
Re: [RESOLVED] Instr function
Glad it helps.
I saw "regular expressions" mentioned above and started gagging, so I had to post something to get the bad taste out of my mouth.
-
Sep 1st, 2014, 03:34 PM
#10
Re: [RESOLVED] Instr function
LaVolpe mentioned a mini word processor. Regular Expressions immediately came to my mind. It is severe overkill for something this simple but it is immensely powerful for things that aren't that simple. It really is like a mini word processor.
-
Sep 1st, 2014, 06:54 PM
#11
Re: [RESOLVED] Instr function
Or like a mini virtual coprocessor programmed in Martian assembly language. Some swear by it, most swear at it.
-
Sep 2nd, 2014, 02:58 AM
#12
Thread Starter
Hyperactive Member
Re: [RESOLVED] Instr function
PS. Honestly, it is one of the most interesting solution I have ever seen. I am sure not many students in informatics or even experienced programmer could solve this problem in a such a simple manner. I have shared this solution to my office colleagues and everybody recognized it is very ingenious. No more words to say. Just hat's off to you !
-
Sep 2nd, 2014, 06:48 AM
#13
Re: Instr function
Your function is not very different than other based on Split function, which is even smaller. After that you can loop the array and if n1=n2 than exit for/sub. The idea is it involves a loop and I wanted to avoid this.
Less code is not per definition more efficient.
Using the Split() would use more memory and processing time.
My code maybe uses more statements but is quite efficient when dealing with large datasets.
-
Sep 2nd, 2014, 09:27 AM
#14
Re: [RESOLVED] Instr function
I have a "tester" in codebank that take a line of VB code and embed code to colorize it, without storing each part but the last part before bound the colorize code. So It works like a filter. If you want only the numbers you can keep numbers for the final output. Also this routine found hex values and floating point numbers, strings, labels (knowing which label is a vb word) and symbols
http://www.vbforums.com/showthread.p...es-for-changes
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|