Ask Your Question

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

asked 2020-08-27 10:25:14 +0100

Arrowman gravatar image

updated 2020-10-02 10:54:24 +0100

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
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2020-08-27 11:15:39 +0100

JohnSUN gravatar image

updated 2020-08-28 10:04:35 +0100

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

edit flag offensive delete link more


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?

Arrowman gravatar imageArrowman ( 2020-08-27 11:34:51 +0100 )edit

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

str = Mid("1234",1,1)
MsgBox str
JohnSUN gravatar imageJohnSUN ( 2020-08-27 12:14:19 +0100 )edit

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.

Arrowman gravatar imageArrowman ( 2020-08-27 13:15:06 +0100 )edit

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()

JohnSUN gravatar imageJohnSUN ( 2020-08-27 13:35:41 +0100 )edit

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.

Arrowman gravatar imageArrowman ( 2020-08-28 09:39:13 +0100 )edit

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.

Mike Kaganski gravatar imageMike Kaganski ( 2020-08-28 10:08:17 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2020-08-27 10:25:14 +0100

Seen: 207 times

Last updated: Oct 02 '20