Is there method startswith in xojo?

Is there any method in the textfield startswith like in Java or Vb.Net in xojo? If there is, how to implement it?

Maybe this :

Or maybe Left:

I think left is the better choice here. Instr will search the whole string for a match.

there is the new BeginsWith

Which only works with the new Text data type.

Textfield.Text.ToText.BeginsWith() if you are using 2014r3 or newer.

easy to make your own… for the old framework

Function BeginsWith(extends aString as string, startString as String) As Boolean Return ( Left( aString, len( startString))=startString ) End Function

also with no case difference :

Function BeginsWithNoCase(extends aString as string, startString as String) As Boolean dim s1,s2 as string s1 = Uppercase( aString) s2 = Uppercase( startString) Return ( Left( s1, len( s2))=s2 ) End Function

String comparisons using “=” are always case-insensitive, so you would not need both of these methods.

Also, many may not realize that Len can be really slow for large strings. LenB with LeftB will return the same result, but will be much faster for larger strings.

return aString.LeftB( startString.LenB ) = startString

If you wanted a case-sensitive comparison, use StrComp:

Function BeginsWithB (Extends aString As String, startString As String) As Boolean
  return StrComp( aString.LeftB( startString.LenB ), startString, 0 ) = 0

Finally, I’ve implemented many of these functions already in the M_String module available on my web site:

I learned this just last week while trying to speed up some string handling routines. I was also using Left / Len and changed to LeftB / LenB and saw the next big improvement compared to my original code. It’s still not as fast as Java or Python’s starts with, but it’s eons faster than InStr.

That has been known for a long time… I always use the “B” functions when speed is an issue and i know the encoding is UTF8.

This is the type of thing I wonder about with the new framework (not used it much yet). There, because you don’t have the String text/Byte duality with the Text class, I worry about speed. There are no “B” functions to give a speed boost if needed.

  • Karen