在 Factor 中,字符串是字符序列,它共享完整的sequences协议:length、nth、head、tail、map、filter等等,此外还拥有自己专属的词汇表。
"hello" length . ! => 5
"hello" reverse . ! => "olleh"
"abcdef" 3 head . ! => "abc"
专门处理字符串的单词大多位于splitting、ascii和unicode:
| 单词 | 词汇表 | 作用 |
|---|---|---|
split1 |
splitting |
在第一次出现处切分 |
split |
splitting |
按集合中的任一字符切分 |
harvest |
sequences |
从切分结果中去掉空片段 |
replace |
splitting |
替换子字符串 |
surround |
sequences |
用前缀和后缀包裹 |
glue |
sequences |
用分隔符连接两个字符串 |
join |
sequences |
用分隔符连接字符串数组 |
>lower |
ascii |
转为小写(ASCII) |
>upper |
ascii |
转为大写(ASCII) |
[ blank? ] trim |
sequences |
去除首尾空白 |
first、second、third、fourth
|
sequences |
取开头的元素 |
first2、first3
|
sequences |
解包开头两个或三个元素 |
>string |
strings |
把字符序列转换成字符串 |
1string |
strings |
把单个字符包装成只含一个字符的字符串 |
CHAR: x |
parser | 单字符字面量(例如CHAR: A → 65) |
如需在数字和字符串之间来回转换,math.parser提供了number>string和string>number。如需格式化字符串输出,formatting提供了sprintf等单词。
如需支持 Unicode 的大小写转换或字符分类,请使用unicode中对应的单词。