[SOLVED] How to get String length and single char in Calc StarBasic Macro

Hello,
does someone know how to achieve these to things:

  • get single character from String
  • get String length

Methods below are wrong:

Dim str As String : str = "123456"
MsgBox "Second char:" &  str(1)
MsgBox "Str length: " & str.Length

You need Mid() and Len()

...
MsgBox "Second char:" &  Mid(str,2,1)
MsgBox "Str length: " & Len(str)
...

This is not specified in the Help, but the ordinal numbers of characters in the string start at 1, not 0
MID().png

Len is working. Thanks!
But Mid is not working when I wan to assignt is to variable.
MsgBox Mid(“12345”,1,1). ’ works like a charm
str = Mid(“1234”,1,1)
MsgBox str ’ error
Error message: Action not supported. Invalid procedure call.

Do you know why?

str = Mid("1234",1,1) MsgBox str “as is”? Without colon :? Try type it in different lines

str = Mid("1234",1,1)
MsgBox str

Sorry, I didn’t noticed that I didn’t used code formating.
This code:

str = Mid("1234",1,1)
MsgBox str 'error here

throws an error.
Error message: Action not supported. Invalid procedure call.

This is strange. Just now I run these two lines and got

MsgBox str.png

Update Oh, I know! Do you try this code without Dim str As String? If you not describe variable str then Basic think that it is a function STR()

Thanks! I know what was a problem. In my original code, I had Mid(<string_variable>,0,1). it is freak but Mid function is counting single chars from 1, not 0! It is little incostistency. Arrays, columns, rows are counting from 0. Could write this information in your post?
Thanks for help, I am marking topis as solved.

It is little incostistency. Arrays, columns, rows are counting from 0

Arrays, columns, rows, and their numbering, are part of LibreOffice UNO API. Mid is BASIC function. They are different things, unrelated to each other, and there’s no “consistency” or “inconsistency” between things defined in different APIs.

Filed tdf#136213.