ALMBeginner
ALMBeginner

Reputation: 21

Find a specific substring within a string VBScript

Dim string1 = "631;27;73"
Dim string2 = "31"

InStr(string1, string2) returns 2 since 31 is a subset of string1. For my current scenario, I want to match the whole number. string2 = 31 is not a matching number in string1 and hence I want to return a zero and append 31 to string1.

Upvotes: 1

Views: 1492

Answers (2)

Ece Kishore
Ece Kishore

Reputation: 11

Try to Do This Code Get Better Result

Dim string1
Dim string2
Dim vCount

string1 = "631;27;73"
string2 = "632"
vCount = 1

vSplit = Split(string1, ";")

For each value1 in vSplit

If value1 = string2 And vCount = 1 Then
Result = "Number Exist"
vCount = vCount+1
ElseIf  vCount = 1 Then
Result = "Number Not Exist"
End If 

Next

MsgBox(Result)

Upvotes: 0

Étienne Laneville
Étienne Laneville

Reputation: 5031

You can split your number list into an array and compare each number:

Function NumberExists(p_sNumber, p_sList)
    Dim arrValues
    Dim iCounter

    arrValues = Split(p_sList, ";")

    For iCounter = 0 To UBound(arrValues)
        If p_sNumber = arrValues(iCounter) Then
            ' Number is found
            NumberExists = True
            Exit Function
        End If
    Next

    NumberExists = False

End Function

In your scenario, you can use the function like this:

Dim string1
Dim string2

string1 = "631;27;73"
string2 = "31"

MsgBox NumberExists(string2, string1)

Upvotes: 2

Related Questions