Is there a comparable way to use Xojo String.Contains to the VB.Net String.Contains like I’m showing below? I can’t seem to get the syntax right to do the same thing in Xojo.
If MyVariable.ToLower().Contains("box") Then
// Do Something
End If
Is there a comparable way to use Xojo String.Contains to the VB.Net String.Contains like I’m showing below? I can’t seem to get the syntax right to do the same thing in Xojo.
If MyVariable.ToLower().Contains("box") Then
// Do Something
End If
You can use String.IndexOf for that.
var value as String = "Some long text"
if value.IndexOf( "long" ) >= 0 then
'// Do Something
end if
As always, happy to help!
Why don’t use String.Contains?
Var MyVariable as String = "this is a red box"
if MyVariable.Contains("box") then
// Do Something
end if
If you are using Xojo 2022r2 or later (see Paul’s note below).
I didn’t even know that existed and didn’t look since he asked. Learn something new every day. Thanks!
I believe you’re looking for .Lowercase rather than .ToLower i.e.:
if MyVariable.Lowercase.Contains("box") then
// Do Something
end
FYI, String.Contains() was added in 2022r2.
@AlbertoD I was trying to use the .Contains comparison options and failing with that. Just doing it the way you suggested works and I just changed the variable to Lowercase
If ship_to_addr2.Lowercase.Contains("box") Then
Address.SetAttribute("PostBox", ship_to_addr2)
End If
Also note that by default .Contains() is case insensitive, making the .Lowercase or equivalent extra overhead that is not serving a useful purpose. If you need .Contains() to be case sensitive, it supports that too by adding as second argument as described in the docs for the call.
Years ago I wrote my own extension method for String.Contains, nice to see that Xojo and I think alike once in a while
A String comparison by default (without optional ComparisonOption parameter) is not case sensitive. You don’t need the lowercase therefore.
I also didn’t know Compare exists until I read this thread.
In a project I use IndexOf quite a lot and I now thought I could replace it by Compare. So I did a quick speed comparison – but in my test scenario Compare needs about twice the time of IndexOf. As I want to keep the app as “snappy” as possible and there is a lot of comparing going on I’ll stick to IndexOf according to this result.
That’s interesting. I would have hoped that Xojo would have optimised the call to Contains() if the optional case sensitivity parameter was omitted so it used the existing framework to keep the speeds the same. I can understand why Contains() with case sensitivity enabled is slower.
Contains() calls through to IndexOf() so I’m surprised at that speed difference. Perhaps there is something we can do about it, though. If you could create a case with a quick sample project showing the difference, I’d like to take a look at it for 2022r4.
Unfortunately I don’t know how to upload a sample project here.
I just made a simple app with two buttons in an empty window. In the Pressed event of the first button I wrote:
Var s As String = "Das ist eine Testapp mit einem Teststring."
Var b As Boolean
Var t As Double = System.Microseconds
For i As Integer = 0 To 100000
b = s.IndexOf("ist") >= 0
Next
System.DebugLog(Format((System.Microseconds - t) / 1000, "####.##") + " ms")
The Pressed event code of the second button is identical with the exception of ‘s.Contains(“ist”)’ instead of ‘s.IndexOf(“ist”) >= 0’.
Did you try the Upload button on the toolbar for composing these messages?
You cannot upload projects to the forum directly (use a 3rd party service for that).
I wasn’t clear, but I meant create an Issue with a sample project so that I can get it assigned to 2022r4 to look at.
Just to be even clearer. You can obviously upload projects to the Issues system.