RegEx not working as expected

Var i As Integer = txtAccntNum.Text.IndexOfBytes( "-" ) // Find the hyphen...

// The code that does magical stuff...
// If a hyphen doesn't exist then add one...
If( i = -1 ) Then
  txtAccntNumOld.Text = txtAccntNum.Text + "-01"
Else
  txtAccntNumOld.Text = txtAccntNum.Text
End If

I am trying to find the hyphen in a numeric string.
Example #1: 444333-01
Example #2: 3432-03
Example #3: 12367-02

When I step through the debugger i is always -1 even though there is a hyphen in the text being examined. I have been reading through the docs but so far I have yet to find the right combination to get the result I want.

You aren‘t using RegEx at all but that’s probably irrelevant. I’m also not sure why you are using IndexOfBytes rather than IndexOf but that’s irrelevant either …

Are you sure the string you check for a hyphen really contains what you think it does? Because I see no reason why your code shouldn’t yield the expected results.

It does because I type it in prior to executing this code. I will try the IdexOf and see if that works. I just realize also that my title is incorrect… sorry.

Your code works correctly here.
Sample project
johncode.xojo_binary_project.zip (5.7 KB)

Not sure where/why are you seeing that i is always -1

If I use the TextFieldName without a .Text it gets all fussy…

“There is more than one method with this name but this does not match any of the available signatures.”

I changed it to IndexOf and it’s working now. Thank you.

yeah - sorry. I didn’t realize it was the name of the TextField. You do need .Text in your case.

Yeah, but does txtAccntNum.Text contain what you just typed? (I assume that txtAccntNum is a DesktopTextField or DesktopTextArea?)

Yes, I typed in 124305-07 and it worked as designed. But it didn’t start working until I changed it from IndexOfBytes to IndexOf.

Strange. Are you working on mac or windows? What version of Xojo?
It worked here with mac and 2026r1.1 using IndexOfBytes (see sample project above)

Linux.

Can you test the project I uploaded and confirm that you get -1 always with IndexOfBytes?
Could it be that you see - but could be a different character like −?

Wouldn’t this have something to do with the encoding of the text in the field? If it’s UTF-8, for instance, using indexOfBytes wouldn’t work since each character takes up two bytes.