我试图只从文本文件中提取非常简单的单词:
Please note that you still have an unclaimed iPhone 7.
We have repeatedly written to you regarding your delivery details. We do not understand why you have not yet confirmed your shipping information so we can send it to your home address.
Your special price for the brand new iPhone 7 phone is only £3 with shipping.
We hope that you'll confirm your information this time.
Run Code Online (Sandbox Code Playgroud)
我一直在使用这个函数,但似乎它抛出一个异常("方法拆分没有重载匹配"):
let wordSplit (text:string) =
text.Split([|' ','\n','\t',',','.','/','\\','|',':',';'|])
|> Array.toList
Run Code Online (Sandbox Code Playgroud)
在F#中,数组或列表中的项由;(分号)字符分隔,而不是,(逗号).您的代码正在创建一个包含一个 10项元组的数组.如果您想要一个包含十个项目的数组,则应编写以下内容:
let wordSplit (text:string) =
text.Split([|' ';'\n';'\t';',';'.';'/';'\\';'|';':';';'|])
|> Array.toList
Run Code Online (Sandbox Code Playgroud)
如果您还希望在分割操作中不返回空字符串,那么您希望其版本String.Split带有StringSplitOptions参数:
let wordSplit (text:string) =
text.Split([|' ';'\n';'\t';',';'.';'/';'\\';'|';':';';'|], StringSplitOptions.RemoveEmptyEntries)
|> Array.toList
Run Code Online (Sandbox Code Playgroud)
请注意,StringSplitOptions它位于System命名空间中,因此如果open System文件顶部没有行,则需要添加一行.