Anonymous user
Tokenize a string: Difference between revisions
→{{header|OCaml}}
(→{{header|Java}}: Updated the for-loop to use the proper variable name (words - not word).) |
|||
Line 875:
;;</lang>
But both of these will process extraneous String.sub (so one string alloc) to generate the "rest of the string" each time to pass to the next call. For N tokens there will be (N - 2) unneeded allocs. To resolve this here is a version which
<lang ocaml>let split_char sep str =
let
with Not_found -> None▼
▲ let i = succ(String.index_from str i sep) in
indices (i::acc) i▼
▲ with Not_found ->
(String.length str + 1) :: acc▼
in
let
let w = String.sub str i (i' - i) in
▲ | _ -> acc
in
aux 0 []
Splitting on a string separator using the regular expressions library:
|