Home > Mobile >  Instr() function with embedded quotation marks
Instr() function with embedded quotation marks

Time:07-27

I have 2 strings and i am trying to find str1 in str2.

Str1= ""products":[]"

str2= "spConfig": {"attributes":{"149":{"id":"149","code":"pack_size","label":"Size","options":[{"id":"79","label":"SINGLE CIGAR","products":["680"]},{"id":"80","label":"PACK OF 3","products":[]"

my

instr(1,str2,"'products':[]") 

returns 0 and I am not sure why. Many thanks.

CodePudding user response:

You need to surround whatever your string is in quotations:

MyStringNumber1 Becomes -> "MyStringNumber1"

You also need to replace existing quotation marks with double quotes so VBA knows to include a quotation mark there:

My"String"Number1 Becomes -> "My""String""Number1"

Or going in the other direction:

Debug.print """My""StringNumber1" will print "My"StringNumber1

Debug.print "My""""StringNumber""""1" will print My""StringNumber""1

To answer you question, I believe this is what you're trying for here:

Option Explicit
Sub ExampleSub()

Dim Str1 As String
Dim Str2 As String

Str1 = """products"":[]"
Str2 = """spConfig"": {""attributes"":{""149"":{""id"":""149"",""code"":""pack_size"",""label"":""Size"",""options"":[{""id"":""79"",""label"":""SINGLE CIGAR"",""products"":[""680""]},{""id"":""80"",""label"":""PACK OF 3"",""products"":[]"""

Debug.Print InStr(1, Str2, Str1)
    'Immediate Window Output = 176

End Sub

Please note I had to replace all your single quotes with double quotes.

  • Related