Anyone using dot notation with strings?

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
JumpyVB
Posts: 75
Joined: Friday 11th September 2020 9:09am

Anyone using dot notation with strings?

Post by JumpyVB »

If I declare a String and ask for it's length in dot notation, the ascii version of the function is used - I find this unexpected! What's the logic behind this? Is the reason historical and related to backwards compatibility?
  Dim TmpStr As String = "Benoît"
  Print "String.Len() = " & String.Len(TmpStr) '6
  Print "Len() = " & Len(TmpStr) '7
  Print ".Len = " & TmpStr.Len '7 !!?? I would have expected the UTF8-version of the len function to be used here. 
If I start typing .Left on gambas code editor, then a help window will pop up (http://gambaswiki.org/wiki/lang/left) stating the following: "This function only deals with ASCII strings. To manipulate UTF-8 strings, use the String.Left class." But if I start typing .Len, then no such warning is shown on the help screen (https://gambaswiki.org/wiki/comp/gb/_boxedstring/len).
User avatar
BruceSteers
Posts: 1589
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Anyone using dot notation with strings?

Post by BruceSteers »

Yeah if you want to deal with non ASCII strings use String class static methods.

Len(TmpStr) is not a static gb.util/String.class method it's a built in function like Mid() and InStr().

If you think something should be added to a wiki page ,,, add it 😉
If at first you don't succeed , try doing something differently.
BruceS
User avatar
thatbruce
Posts: 170
Joined: Saturday 4th September 2021 11:29pm

Re: Anyone using dot notation with strings?

Post by thatbruce »

I believe that Bruce is trying to say that declaring a variable as a String is not the same as using the String class even though it is in the gb component.'
The gb.String class is a set of static utility functions to specifically handle UTF character strings, a String variable is a "normal" ASCII string.
Since gb.String is essentially a static "class" i.e. it has no visible attributes, you cannot declare an object of that type, as is clearly indicated by the "This class is static." comment in the wiki.
I see no need for any changes to the wiki.
hth
bruce
Have you ever noticed that software is never advertised using the adjective "spreadable".
User avatar
BruceSteers
Posts: 1589
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Anyone using dot notation with strings?

Post by BruceSteers »

I guess it comes down to Len() being a built in function and String.Len() being in the "handler of utf-8 strings" String.class from gb.util

With your logic then theoretically also Mid() and InStr(), etc would be expected to work UTF-8 strings too?

But no I think it makes sense.

As it stands Mid(), InStr(), Len(), etc are all for plain ascii
String.Mid(), String.InStr(), String.Len(), etc are all for UTF-8

Respects
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1128
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Anyone using dot notation with strings?

Post by cogier »

But if I start typing .Len, then no such warning is shown on the help screen (https://gambaswiki.org/wiki/comp/gb/_boxedstring/len).
I have added the same warning to the Wiki. This should show up in a day or 2.
Post Reply