Empty string with 'segment text' block. Why?

The debug block simply calls up a notifier window with an 'OK' button, that shows me the contents of the text parameters I pass to it.

The 'debug 000000' block shows me that strEncodedText contains "0E0A~"

But 'segment text, text = "0E0A~", start = 1, length = 1' block gives me an empty string in strCh according to 'debug 11111111' block

Why? What am I doing wrong?


String handling in MIT app inventor is a mess!

There is no way I can SIMPLY iterate through the characters of a string using the 'segment' block.

I need to ridiculous amount of if statements to avoid index out of bounds errors that crash the app.

And the 'length' attribute in the 'segment' block is simply wrong!

It is not the length of the desired string. It should be called 'End pos'' because that is how it works.

Start = 1, Length = 1 does not result in a single character string but rather an empty string.


Show the code from your debug procedure. And please post bigger blocks. They are not readable.

Exactly! Because 'Length' should be called 'End Pos'
As in how the parameters of 'C' substring(...) work.

What do you mean by exactly. It comes back as "0" exactly what i expected it to be.

Length means length of the resulting string.
So a substring starting at index 1 of length 1 makes perfect sense - it is a single character string with the character at index 1.
With 'C' substring(int StartPos, int EndPos) it is a substring starting at StartPos and ending at EndPos but not including the character at EndPos.
If you did substring(1, 1) then you would get an empty string.
To get a 1 character long string you would need to substring(1, 2) - that is how the segment block is working. So 'length' should be 'endpos'.
Using 'length' as the name of the second parameter is confusing.

So you are trying to loop through a string? Can you explain what you are trying to do and show all your blocks? Still curious about what your debug procedure does.

Peter's explanation is indeed correct,
your block is returning character 0 because it is your first character (not zero as empty).
if you want to use length, use the following block:

Use the tool tip for the segment block, and forget your C expectations.

An easy way to iterate through the letters of a piece of text:

set a local list L to (split input_text at "")
if item 1 of list L is empty text remove it from list L (a peculiarity of the AI2 split block)
for each item in list L
   process item (a single letter)
end for each

P.S. the AI2 segment block hates empty text.