1

I'm slightly confused. I've written a small piece of a code that reads a string and outputs if the word is present in the string. The below code returns me 1 as expected

    Sub FindSomeText()
  MsgBox InStr("Look in this string", "Look")
End Sub

When i used the same logic on the below code but with a longer string i get a response of 16? instead of 1. Why would this be?

 Sub SearchAString()

  MsgBox InStr("Search through this random string and find how many strings are similar", "this")

End Sub
NewCoder
  • 35
  • 6
  • Yep, @OlivierDepriester is right and answered quicker than me. I can add a link for reference https://www.w3schools.com/asp/func_instr.asp – PeaceAndQuiet Apr 23 '20 at 16:28
  • 2
    Just return a boolean - `HasValue = (InStr("...", "...") > 0)` – user692942 Apr 23 '20 at 16:48
  • 3
    Does this answer your question? [Search a string in VBScript to verify if contains a character](https://stackoverflow.com/questions/37168882/search-a-string-in-vbscript-to-verify-if-contains-a-character). Not the first time this has been covered, try searching via a search engine before posting. – user692942 Apr 23 '20 at 16:51

1 Answers1

2

You are looking for the string this. In the string Search through this ..., the term this starts on the 16th position. There is no issue with the way you use it.

Instr returns the position of the searched term and not a boolean saying if the term is present or not.

If the searched term is not found, Instr will return 0

Olivier Depriester
  • 1,615
  • 1
  • 7
  • 20
  • How would i change it so it returned 1 after it searched the string? – NewCoder Apr 23 '20 at 16:25
  • You can use ```Iif(Instr("....", ,"...) < 0, -1, 1)```. It is similar to ```If Instr("...", "...) < 0 Then result = -1 Else result = 1 End If ``` – Olivier Depriester Apr 23 '20 at 16:33
  • Suggest `HasValue = (InStr("...", "...") > 0)` is cleaner, that way you have a reusable boolean value. Also using `-1` and `1` for return values doesn't fit representation of a boolean in VBScript, which is `-1` for `True` and `0` for `False`. – user692942 Apr 23 '20 at 16:50
  • 1
    Also, `InStr()` returns `0` not `-1` if the string is not found, so `> 0` is sufficient. - see [The Documentation](https://learn.microsoft.com/en-us/previous-versions//wybb344c%28v%3dvs.85%29). – user692942 Apr 23 '20 at 16:57
  • @Lankymart My bad, I was stuck on this wrong idea. I have fixed my answer – Olivier Depriester Apr 23 '20 at 17:10
  • No matter, it's been [answered before](https://stackoverflow.com/questions/37168882/search-a-string-in-vbscript-to-verify-if-contains-a-character). – user692942 Apr 23 '20 at 17:13
  • Yes the above returns true or false if the word in the string. But what if from that string i want to specifically say how many times does the word 'Like' appear in a string. – NewCoder Apr 24 '20 at 08:44
  • @NewCoder Use a `For` loop *(`1` to `Len()` of the search string)* with `InStr()`, store the position returned by `InStr()` and keep track of how many times the word is found. – user692942 Apr 24 '20 at 08:55
  • ```Instr``` has a ```start``` argument. Thus, you can loop on an ```Instr``` call until it returns 0. In the loop, each ```start``` value must be the previous ```Instr``` result + 1. so the first call will be ```Instr(1, "...", "this")``` and will return 16, next call will be ```Instr(17,"...", "this")```` ... and so on – Olivier Depriester Apr 24 '20 at 08:58
  • I've written the following but it keeps returning 0 instead of 1 MyString = ("hello") CharacterCount = Len(MyString) - Len(Replace(MyString, TargetCharacter, "hello")) MsgBox CharacterCount – NewCoder Apr 24 '20 at 10:42
  • @NewCoder like I said earlier, search - [Find all strings within a string](https://stackoverflow.com/a/35244353) – user692942 Apr 24 '20 at 12:02