Search code examples
splitocamlstring-parsing

Does OCaml have String.split function like Python?


I am using this to split strings:

 let split = Str.split (Str.regexp_string " ") in
   let tokens = split instr in
 ....

But the problem is that for example here is a sentence I want to parse:

pop     esi

and after the split it turns to be (I use a helper function to print each item in the tokens list):

item: popitem: item: item: item: esi

See, there are three spaces in the token list.

I am wondering if there is a string.split like in Python which can parse instr this way:

item: popitem: esi

Is it possible?


Solution

  • Don't use Str.regexp_string, it's only for matching fixed strings.

    Use Str.split (Str.regexp " +")