Problem with Left, Mid, Right using alternative syntax

I’ve only just realised that I am using the possibly “old-fashioned” way of manipulating strings using Left, Mid, Right etc as I did in Real Basic. I usually write:

aString = mid(anotherString, integer, integer)

and usually it seems to work fine although I have had some inexplicable problems.

One problem I have had involved checking a string for a character I use to set the colour of a rectangle created using a runtime cloning method. I couldn’t see what was wrong with my code, then I noticed there was a different way of doing it (I was actually using Left and not Mid)

aString = anotherString.Left(integer, integer)

When I used the second method instead, everything worked fine. Is the first syntax deprecated or is there some reason it will not work in some cases while the other method will?

I was simply looking for a capital B with

x = Left(ch, 1, 1)
If x = “B” then etc etc. I just changed it to

x = ch.Left(1, 1)
If x = “B” then etc etc

I have more than 100 points in my code where I have used the first syntax and it will be irritating to have to go through them all replacing the one method with the other just in case.

In the transition to API 2.0 Mid became Middle and all three functions became 0-based. You’re likely running into old vs new framework problems.

1 Like

We all LURVE to do that.

1 Like

Thanks, so that means I really ought to go back and use the second syntax instead of the first? The current version of Xojo allows me to use either, which it really shouldn’t if either may lead to unsuspected problems. Maybe it would be best if those in charge decided what was the accepted syntax and deprecated the other, or something. It’s taken me ages wondering what I was doing wrong before I noticed there was more than one syntax and tried the second as a last resort.

This is incorrect, only Middle is zero-based. Left and Right take counts, not indexes, as parameters.

Change Mid to Middle was one of the difficult thing to update from API1 to API2. We need to review our code. I discovered until now only one mistake on the 11 project I updated. And when I updated, re-read my code made me discover and correct 2 or 3 errors in my code.

As many things, we are bored before begin to do it and happy when finish.

1 Like

What about the signature with a start position, as OP is using and is undocumented?

Huh, I can’t actually get OP’s Left with two params to work.

I saw that too and didn’t think it was a thing, but didn’t get a chance to check.

1 Like

I have never seen Left with 2 parameters, maybe OP just typed the code on the forum and confused it with Mid/Middle?

Left is ‘left from the start’ and just needs the count we need.

With the documentation being lacking or missing information in so many areas, I had assumed it was simply missing another signature that included a start position. OP had mentioned Mid wasn’t working when they discovered this Left signature.

I made the mistake of trying to help again. Gotta stop doing that.

Is not a mistake Tim, I guess OP is using the code and worked for them but we don’t know the Xojo version.

Xojo 2022r1.1 show this Issue:
image

I wonder which version of Xojo works with String.Left(1,1)

Also tried Left(ch, 1, 1) and it doesn’t work with Xojo2022r1.1 on a Mac. What version of Xojo are you using? Are you using Windows? (I can’t test Windows right now):
image

I’m not clear who is asking questions of whom. I am using Xojo 2022r1.1 on a late 2013 iMac. I have just gone through my current project changing ch = mid(aString, Integer1, Integer2) to ch = aString.mid (Integer1, Integer2) and it hasn’t made the slightest difference. It all works OK.

As I said, it was only when I used the first syntax to search a string for a “B” character right at the start of the string that it didn’t work. I stepped through the code several times in the debugger to try to find out why my crossword cells were not being coloured black when they should be. I could see the string in the variables, I could see the B at the beginning and I watched several times as the code totally ignored it.

When I moved to the second syntax, it worked fine and that’s really all I can tell you. Sorry. Steve

In your original message, you wrote ‘Left’, so you got us confused.

mid(aString, Integer1, Integer2)
is the same as
aString.mid(Integer1, Integer2)
you will get a different result if you use ‘Middle’, you can try using the same Integer1 and Integer2 with
aString.middle(Integer1, Integer2)
to see that you will get a different result. That’s it. Hope this clears things up.

I have never ever used the word Middle - that was mentioned by Tim Parnell.

I used Mid(string, n, 1) for consistency, whether n was the position of a character at the start or the end of the string or somewhere in the middle. I was using the string to represent a number of characteristics of crossword cells (DesktopRectangles) in my project. I used a capital B or a capital W as the first letter of the string to indicate whether the colour of the cell was to be black or white.

When I started having inexplicable problems with getting my project to set the fillcolor of each cell by referring to the string, one of the things I tried was to use Left instead of Mid but it didn’t solve the problem. I couldn’t understand why the colour code characters were being ignored. I could see it happening as I watched the code execute step by step in the debugger.

My difficulty went on for a long time and I thought it may have something to do with the fact that I was using rectangles cloned at runtime, something entirely new to me It was only when I noticed the second form of syntax in the documentation using ch.Mid( and ch.Left( that, I switched over to using that and it all started working properly.

I’ m sorry if I confused anyone but I thought I was being clear.

Thanks anyway - Steve