String length: Difference between revisions

Line 305:
 
<lang zxbasic>10 INPUT a$
20 PRINT LEN( a$)</lang>
 
However, it's not quite as trivial as this.
 
==Byte length==
Strings can contain embedded colour codes; an inline INVERSE (CAPS SHIFT + 4) would be represented as CHR$ 20 + CHR$ 1. The LEN function will account for all these bytes. On the flipside, ZX Spectrum keywords are all tokenised, and there's nothing stopping you using them in a string; " RANDOMIZE ", if the keyword is used, will take a single byte (CHR$ 249) rather than the 11 characters it actually uses. The above version of the code will produce byte length.
 
==Character length==
Stripping out all entries in the string with codes in the lower 32 will get rid of colour codes. The character length of a token is not a simple thing to determine, so this version strips them out too by eliminating anything above CHR$ 164 (the last UDG). A 91-entry DATA list of token lengths might be the next step.
 
<lang zxbasic>10 INPUT a$
20 LET b$=""
30 FOR x=1 TO LEN a$
40 LET k=CODE a$(x)
50 IF k<32 OR k>164 THEN GOTO 70
60 LET b$=b$+a$(k)
70 NEXT x
80 PRINT LEN b$</lang>
 
==={{header|Commodore BASIC}}===
77

edits