将字符串转换为单词数组
Convert a string into array of words
如何将字符串分隔成 list/array 白色 space 分隔的单词。
let x = "this is my sentence";;
并像这样将它们存储在 list/array 中:
["this", "is", "my", "sentence"]
使用标准库 Str split_delim 和正则表达式类型。
Str.split_delim (Str.regexp " ") "this is my sentence";;
- : bytes list = ["this"; "is"; "my"; "sentence"]
强烈推荐 UTop,它非常适合快速搜索库(我输入 Str
,看到它在那里,然后 Str.
并寻找合适的功能)。
完整的过程是这样的:
第一个opam install re
如果你正在使用utop
,那么你可以这样做
#require "re.pcre"
let () =
Re_pcre.split ~rex:(Re_pcre.regexp " +") "Hello world more"
|> List.iter print_endline
然后 运行 它与 utop code.ml
如果你想编译本机代码,那么你需要:
let () =
Re_pcre.split ~rex:(Re_pcre.regexp " +") "Hello world more"
|> List.iter print_endline
注意 #require
是如何消失的。
然后在命令行你会做:ocamlfind ocamlopt -package re.pcre code.ml -linkpkg -o Test
OCaml 网站有大量教程和帮助,我还有一个博客 post 旨在帮助您快速上手:http://hyegar.com/2015/10/20/so-youre-learning-ocaml/
如何将字符串分隔成 list/array 白色 space 分隔的单词。
let x = "this is my sentence";;
并像这样将它们存储在 list/array 中:
["this", "is", "my", "sentence"]
使用标准库 Str split_delim 和正则表达式类型。
Str.split_delim (Str.regexp " ") "this is my sentence";;
- : bytes list = ["this"; "is"; "my"; "sentence"]
强烈推荐 UTop,它非常适合快速搜索库(我输入 Str
,看到它在那里,然后 Str.
并寻找合适的功能)。
完整的过程是这样的:
第一个opam install re
如果你正在使用utop
,那么你可以这样做
#require "re.pcre"
let () =
Re_pcre.split ~rex:(Re_pcre.regexp " +") "Hello world more"
|> List.iter print_endline
然后 运行 它与 utop code.ml
如果你想编译本机代码,那么你需要:
let () =
Re_pcre.split ~rex:(Re_pcre.regexp " +") "Hello world more"
|> List.iter print_endline
注意 #require
是如何消失的。
然后在命令行你会做:ocamlfind ocamlopt -package re.pcre code.ml -linkpkg -o Test
OCaml 网站有大量教程和帮助,我还有一个博客 post 旨在帮助您快速上手:http://hyegar.com/2015/10/20/so-youre-learning-ocaml/