Tinderbox Forum

Attribute or action code for character count

Is there an attribute or action code for character count?

$TextLength gives the (plain text) character length of $Text. Or use String.size for the character length of a string.

1 Like

You could also try using attribute $EstimatedNoteSize as a proxy for character count. Below an example in a test file. Adding an initial “H” to the text field leads to a jump in value from 6 to 431 for the $EstimatedNoteSize attribute. Every subsequent additional character adds 2 to the total e.g. 433, 435…

The action code ($EstimatedNoteSize-429)/2 should then in theory give you the character count for the text field. As I don’t know the algorithm behind the $EstimatedNoteSize attribute I’m not sure how robust this approach is and it would require some experimentation.

image

1 Like

As is noted for $EstimatedNoteSize:

This provides a rough estimate of the space that the note will require on disk.

Given that there methods (posts) above that return accurate character counts, back-calculation from $EstimatedNoteSize is always likely to be less accurate for character counts.

This demo TBX, text-size.tbx (116.6 KB), shows that $TextLength and $Text.size are equal and that both are the character count of the plain text of the note $Text. RTF styling (font type, italic, highlighting) has no effect on the count. But ‘invisible’ characters like space, link break, tab, and such do contribute to the count.

Screen grab from the TBX:

More details:

  • $EstimatedNoteSize takes into account the approximate size of the note text, including images embedded in the text. Its intended use is to identify notes with unexpectedly-large images.

  • $TextLength and $Text.size() now report the length of the UTF-16 representation of the text of the notes. As you likely know, Unicode can represent more than a million distinct characters! The characters that count as “1 character” here are those in the base multilingual plane which include most modern languages and many common symbols. Other characters — notably emoji — count as “2 characters” for $TextLength and $Text.size.

2 Likes

Ah, yes. My bad. I will update notes accordingly.

Done. See: new aTbRef article ‘Counting characters in strings’.