How to make "if text does not contain _ then" in Xojo?

I’m trying to make a web browser that can search with Google if the text isn’t a URL. It’s pretty simple. If the text doesn’t contain “.” (or something else) I want it to search with Google.

Here’s what I have so far:

If TextField1.text does not contain "." then
  HTMLViewer1.LoadURL("https://www.google.com/search?q="+TextField1.text)
Else
  HTMLViewer1.LoadURL("https://"+TextField1.text)

Any help with this?

If TextField1.Text.IndexOf(".") = -1 Then

https://docs.xojo.com/String
https://docs.xojo.com/String.IndexOf

1 Like

That works beautifully. Thanks!

If you do this a lot, you might want to write a string extension function:

function DoesNotContain(extends s as string, target as string) as boolean
  return s.IndexOf(target) = -1
end function

If TextField1.Text.DoesNotContain(".") then
which might be easier to read and understand.

You could even write it so it extends the textField:

function DoesNotContain(extends tf as TextField, target as string) as boolean
  return tf.text.IndexOf(target) = -1
end function

Then your code is simply:
If TextField1.DoesNotContain(".") then

2 Likes

Hey, there, I’m back.

What if I wanted a specific word to go to a specific website.
For example, I want “dneail/stream” to go to dneailstream.com.

And if its possible, can I make a label display the current webpage.

You may want to read this Xojo blog post, “Updated Tutorial: Active Words”, and check out the downloadable project.

1 Like

That’s not what I wanted.

If I type “dneail/stream” into the text box, I want HTMLViewer1 to take me to dneailstream.com

My apologies. I figured it out.

The code I used what

If SearchField1.Text="<text>" Then
  HTMLViewer1.LoadURL("<url>")
Else

then continue this process