len(text) does not work for umlauts
-
https://fuzearena.com/help/view/len
print("len1=",len("äöü"), " len2=",len("aou"))
prints:
len1=6 len2=3
probably it checks the byte size 🤔
-
found a workaround: hope this is not introducing problems.
function strLen(text) textLength = 0 charLength = textWidth("a") textLength = int(textWidth(text) / charLength) return textLength
example:
print("len1=", strLen("äöü"), " len2=", strLen("aou"))
prints:
len1=3 len2=3
-
Ok, sometimes, I would still need len. For example in this case, it works very well:
t1 = "äöüä" t2 = "" for i = 0 to len(t) loop t2 += t1[i] repeat print(t2)
output:
äöüä
len(t1) is 8 but, the combined bytes result again in the correct string.
So, maybe its nice for certain cases, to have a function to count the chars, and keep len() for the bytes.
So, this is super low prio. But maybe good to know. -
I know that len is in the help section for text handling but I'm not so convinced this is a bug as a missing command from the language.
You often have
len()
for getting the length of arrays and the like, and thenstrLen()
for specifically dealing with this scenario of multi-byte characters in strings.I'll defer to the rest of the team to decide how they want to handle this? @Luke @Jonboy @Dave @pianofire
-
@spikey said in len(text) does not work for umlauts:
found a workaround: hope this is not introducing problems.
function strLen(text) textLength = 0 charLength = textWidth("a") textLength = int(textWidth(text) / charLength) return textLength
example:
print("len1=", strLen("äöü"), " len2=", strLen("aou"))
prints:
len1=3 len2=3
For some reason, when you have set the text size to 37, this function no longer works.
textSize(37) print(strLen("apa"))
prints 2 instead of 3. Change the text size to 36 or 38, and it works as expected!
I used your high score code in my Space Worm game, and for no apparent reason, the high score table always displayed wrong. Took me ages to locate the problem!
Very weird!
-
@vinicity sry, to hear. Very interesting though. And glad you found the bug👍
-
@vinicity I based the
strLen()
now on the byte size of a character and dropped the workaround over the pixel length of a string. https://fuzearena.com/forum/topic/1439/demo-high-scores-supports-now-unicode . Dividing the total pixel length through a single character pixel length caused a rounding error at some specific text sizes and 2.99999990 resulted in a string length of 2. This wont happen anymore now.