HyperTalk | HT Strings |
---|---|
Characters: | |
char 1 of aString | aString.Char[0] |
char 1 to 4 of aString | aString.char[0:4] |
the number of chars in aString | aString.Num.Chars |
Words: | |
word 1 of aString | aString.Word[0] |
word 1 to 4 of aString | aString.Word[0:4] |
the number of words in aString | aString.Num.Words |
Lines: | |
line 1 of aString | aString.Line[0] |
line 1 to 4 of aString | aString.Line[0:4] |
the number of lines in aString | aString.Num.Lines |
Items: | |
item 1 of aString | aString.Item[0] |
item 1 to 4 of aString | aString.Item[0:4] |
the number of items in aString | aString.Num.Items |
set the itemdelimiter to "/" | aString.ItemDelimiter = "/" |
Sentences: | |
aString.Sentence[0] | |
aString.Sentence[0:4] | |
aString.Num.Sentences | |
aString.IgnoreAbbrevs = ["mr", "mrs", "ms", "dr", "esq"] | |
Combinations (Chaining): | |
word 2 of line 7 of aString | aString.Line[6].Word[1] |
line 3 of char 60 to 245 of aString | aString.Char[59:245].Line[2] |
the number of items in word 5 to 10 of aString | aString.Word[4:10].Num.Items |
etc. | etc.:-) |
The HT Strings project aims to provide a simple Python library that implements string handling similar to what HyperTalk provided. HyperTalk handled string manipulation using a fairly "natural" language approach based on measuring strings in chunks (by characters, lines, words and items). So, for example, one could ask for word 1 to 4 of a string and get back a substring containing all the characters from the first character of the first word to the last character of the 4th word in the string.
The HT Strings library provides a "pythonic" version of this capability. Where HyperTalk used an English-like syntax, HT Strings uses Python's slice and index idioms instead. Using the example from above we'd get aString.Word[0:4]
to get word 0 to 3. One really important thing to understand about how this library works is to understand that the result is NOT a list or tuple of strings, but a substring with all the characters (even whitespace) intact from the original string. Another example should make this clear: htstring("Sample string with unusual spacing.")[0:4]
will return htstring("Sample string with unusual")
. One powerful way that chunks could be used in HyperTalk was that they could be chained together at will. HT Strings retains this capability as well (see the examples in the table).
There is also an additional chunk for sentences (as opposed to lines) that wasn't provided for in HyperTalk. Please note, however, that, because "." is used ambiguously in English (for abbreviations), that this may not always return what you'd expect. There is an IgnoreAbbrevs property that allows you to set a list of abbreviations after which a "." should be ignored but this really only takes you so far.