Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 657
lllllllllllll Avatar asked Apr 21 '14 19:04

lllllllllllll


People also ask

How do I split a string into string?

Use the Split method when the substrings you want are separated by a known delimiting character (or characters). Regular expressions are useful when the string conforms to a fixed pattern. Use the IndexOf and Substring methods in conjunction when you don't want to extract all of the substrings in a string.

What is split () function in string?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

Is Split a string method in Python?

Python String split() MethodThe split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.


4 Answers

Since OCaml 4.04.0 there is also String.split_on_char, which you can combine with List.filter to remove empty strings:

# "pop     esi"
  |> String.split_on_char ' '
  |> List.filter (fun s -> s <> "");;
- : string list = ["pop"; "esi"]

No external libraries required.

like image 138
glennsl Avatar answered Sep 30 '22 02:09

glennsl


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

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

like image 36
Jeffrey Scofield Avatar answered Sep 30 '22 03:09

Jeffrey Scofield


Using Jane Street's Core library, you can do:

let python_split x =
  String.split_on_chars ~on:[ ' ' ; '\t' ; '\n' ; '\r' ] x
  |> List.filter ~f:(fun x -> x <> "")
;;
like image 44
Anthony Scemama Avatar answered Sep 30 '22 02:09

Anthony Scemama


This is how I split my lines into words:

open Core.Std
let tokenize line = String.split line ~on: ' ' |> List.dedup

Mind the single quotes around the space character.

Here's the documentation for String.split: link

like image 20
Matthias Braun Avatar answered Sep 30 '22 03:09

Matthias Braun